2011-04-13 13 views
1

私たちのホームページには、国別のページに移動するドロップダウンメニューがあります。jquery .change function

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

いくつかのコードサンプルを投稿できますか? –

+0

文書の同じコードを実行して、変更イベントで実行する準備はできていますか? –

+0

ねえ、それはdocument.readyの中にあります – AJFMEDIA

答えて

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

変更機能を割り当てた後で、手動で変更イベントをトリガすることもできますが、少しはっきりしているように、あなたのやり方が気に入っています。 – Wiseguy