2012-07-22 71 views
0

我有一些代碼在頁面中加載城市列表時,我更改國家選擇框。jQuery.when()。然後()不與選擇更改事件

我想在頁面中使用流動的代碼設置默認值,但.then不工作後的jquery。有任何想法嗎?

該國工作完美,但城市沒有加載。

jQuery(window).load(function() { 
    jQuery(document).ready(function(){ 
     jQuery('#jr_country').val('united-kingdom'); 
     jQuery.when(jQuery('#jr_country').change()).then(jQuery('#jr_city').val('london')); 
    }); 
}); 

回答

0

$.when()接受的jquery $.Deferred對象,而.change()返回jquery對象實例。

如果你想在你的代碼中使用的承諾 - 它改寫成類似:

var d = jQuery.Deferred().then(...); 
jQuery('#jr_country').change(d.resolve); 

工作例如:http://jsfiddle.net/zerkms/QuU3f/

+0

嗨非常感謝我不知道我得到如何實現延遲的jQuery('#jr_country')。它正在生成城市選擇,所以我必須先運行jQuery('#jr_city')。val('london') – 2012-07-23 00:02:25

+0

@ open-ecommerce.org後的變化:這裏是一個很好的閱讀:http:/ /joseoncode.com/2011/09/26/a-walkthrough-jquery-deferred-and-promise/ – zerkms 2012-07-23 00:03:01

+0

非常感謝我認爲我有一個問題,我的網頁的邏輯。我會研究你寄給我的鏈接非常感謝 – 2012-07-23 00:19:54

0

您可以在指數評估

$("#select_id").change(function() { 
    if ($("#select_id")[0].selectedIndex == 1){ 
     // Do something 
    } 
    }); 

和您可以通過

$("#select_id").val();