0

私が使用する jQuery 関数は、1 つのページで同じ選択値を選択する可能性を無効にします。例: ページ 2 に異なる選択があり、最初の選択で選択した値が 1 の場合、2 番目の選択では値 1 を無効にする必要があります。コメント付きの jQuery 関数を見つけましたが、それが機能している方法がわかりません。

var previous = -1;
// Setting a previously selected value
$("select").change(function () {
// When any select element is changed
if ($(this).val() > -1) {
// When value is > -1
    // Get all selects but not the current one which triggered the change event
    $("select").not(this)
       .find("option[value=" + $(this).val() + "]").attr('disabled', 'disabled');
    // set the current option value to disabled

     // Get all selects but not the current one which triggered the change event
    $("select").not(this)
        .find("option[value=" + previous + "]").removeAttr('disabled');
    // Remove the disabled property on previous value
} else {
    // Get all selects but not the current one which triggered the change event
    $("select").not(this)
        .find("option[value=" + previous + "]").removeAttr('disabled');
    // Remove the disabled property
}
}).focus(function () {
previous = $(this).val();
// store the current value of the select to previous 
}); 

ここに JSFiddle リンクがあります: http://jsfiddle.net/Z2yaG/4/

誰かが私にvarの前の配列であることを説明できますか? 関数は以前の変数のすべての選択からの値をどのように保存しますか?

4

1 に答える 1

1

選択にフォーカスするpreviousと、選択値を取得します。変更すると $(this).val()、変更された値が取得されprevious、以前の値 (変更が行われる前の値) が含まれます。

したがって、基本的に関数が行うことは、オプションを選択するとすぐに、他の2つのオプションでそのオプションを無効にすることですselects

$("select").not(this) .find("option[value=" + $(this).val() + "]").attr('disabled', 'disabled');

この行は、select現在のものを除くすべてを選択しdisableoption値を持つを意味しますthis.val()

previous次に、オプションが選択されていないため、同様にオプションを有効にします

$("select").not(this) .find("option[value=" + previous + "]").removeAttr('disabled');

selectsを除くすべてを選択し、選択した値を持つすべてのthisを削除しますdisableoptions

于 2013-06-27T10:22:52.430 に答える