2011-04-13 128 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