2011-04-13 72 views
1

在我們的主頁上,我們有一個下拉菜單,可以將您帶到特定國家/地區的頁面。jquery .change功能

我們使用jQuery的.change功能時,在下拉框中選擇國內用戶指向正確的國家。

如果用戶選擇的國家,並查看該國網頁後壓回,然後希望再次查看同一個國家的頁面,他們可以選擇其他國家,按返回,然後選擇之前期望的國家。 反正這個?

$(document).ready(function() { 
$('select.jumpmenu').change(function(){ 
     if($(this).find("option:selected").val() != 'no_selection'){ 
      $.ajax({ 
        type: "GET", 
        url: "/countries/jsontariff/" + $(this).find("option:selected").val(), 
        success: function(html){ 
         window.location = "/recommend/"; 
        } 
      }); 
     }  
    }); 
}); 
+0

你可以張貼一些代碼樣本? – 2011-04-13 15:19:32

+0

如何在文檔上執行相同的代碼,以便在更改事件上執行? – 2011-04-13 15:23:07

+0

嘿,它在document.ready中 – AJFMEDIA 2011-04-13 15:28:40

回答

1

嘗試重置選中onload:

$(document).ready(function(){$('#selectList option:first').attr('selected',true)}) 
0

我會在頁面加載執行相同的代碼。

executeCountrySelection() { 
    if($('select.jumpmenu').find("option:selected").val() != 'no_selection'){ 
     $.ajax({ 
       type: "GET", 
       url: "/countries/jsontariff/" + $(this).find("option:selected").val(), 
       success: function(html){ 
        window.location = "/recommend/"; 
       } 
     }); 
    } 
} 

$(document).ready(function() { 

    //executes when the page loads 
    executeCountrySelection(); 

    //then again when the select is changed 
    $('select.jumpmenu').change(function() { executeCountrySelection(); }); 

}); 
+0

你也可以在分配更改函數後手動觸發change事件,但我更喜歡你的方式,因爲它看起來更清晰和直觀。 – Wiseguy 2011-04-13 15:29:59