1

私はJSの初心者として、選択メニューの「無効」機能を表示および非表示にする非常に単純なスクリプトを作成しました。

selectmenuのjQueryUIをロードするまで、すべてが正常に機能します。その後、それはもう動作しません。

私はそれを解決するために一日中試みましたが、できません。おそらく誰もが私を助けることができます。

// Metadata
$('#year').change(function(){
    var sel = $(this); 
    var val = sel.val();

    if(val == "2008")
    {
         $('#c2').find("option:eq(1)").removeAttr("disabled");
    }
    else
    {
        $('#c2').find("option:eq(1)").attr("disabled", "disabled");

    }

});

何が起こるかというと、誰かが#yearから「2008」を選択すると、#c2のオプション1が有効になり、その逆も可能になります。

ここでは可能と思われますが、スクリプトを自分の試行錯誤に限定することはできません。

更新されたコード

<script>
// Metadata
$("#year").selectmenu({

    // listen to the select event of the custom menu, not the original dropdown
    select: function(){
        var val = $(this).val();

       if(val == "2008"){
             $("#c2").find("option:eq(2)").removeAttr("disabled");
        }
        else{
            $("#c2").find("option:eq(2)").attr("disabled", "disabled");
        }

        // must call this to reflect the change
        $("#c2").selectmenu("refresh");
    }
});
</script>
4

2 に答える 2

2

リンクしたデモページのソースコードを見ると、refreshメソッドを呼び出してselectmenuを更新する必要があるようです。

デモソースからの例:

files2.find("option:eq(0)").attr("disabled", "disabled");
files2.selectmenu("refresh"); // <-- this

これがあなたが探しているものだと思います:http://jsfiddle.net/zuXSU/2/。メニューを変更すると、オプションの1つが有効/無効になります。

関連コード

$("#year").selectmenu({

    // listen to the select event of the custom menu, not the original dropdown
    select: function(){
        var val = $(this).val();
        alert(val);

        if(val == "2008"){
             $(this).find("option:eq(1)").removeAttr("disabled");
        }
        else{
            $(this).find("option:eq(1)").attr("disabled", "disabled");
        }

        // must call this to reflect the change
        $(this).selectmenu("refresh");
    }
});
于 2012-08-16T13:13:27.380 に答える
0

これを試して

 $("#year").enableSelection();

これはあなたを助けるかもしれません

于 2012-08-16T14:02:30.093 に答える