Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

javascript - disable select option value when selected

I am using this code. But I'm facing a problem that when I select "one" in 1st select box and then in 2nd I select "two" and when I select again 2nd value to "three" but the value "two" remain disabled:

HTML code

// one
<select>
    <option>Select</option>
    <option value="1" >one</option>
    <option value="two">two</option>
    <option>three</option>
</select>
//two
<select>
    <option>Select</option>
    <option value="1" >one</option>
    <option value="two">two</option>
    <option>three</option>
</select>

This is JavaScript code:

$(document).ready(function(e) {

    var $selects = $('select#players');
    available = {};

    $('option', $selects.eq(0)).each(function (i) {
        var val = $.trim($(this).text());
        available[val] = false;
    });

    $selects.change(function () {
        var $this = $(this);
        var selectedVal = $.trim($this.find('option:selected').text());

        available[selectedVal] = true;

        $selects.not(this).each(function () {
            $('option', this).each(function () {
                var optText = $.trim($(this).text()),
                    optCheck = available[optText];
                if(optCheck) {
                    $(this).prop('disabled', true);
                }
                else {
                     $(this).prop('disabled', false);
                }
            });
        });
    }).change(); // Trigger once to add options at load of first

});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming you want to disable values in the other select-boxes once chosen, here is a sample.

<select>
    <option value="" selected="selected">Select</option>
    <option value="1">one</option>
    <option value="2">two</option>
    <option value="3">three</option>
</select>
<select>
    <option value="" selected="selected">Select</option>
    <option value="1">one</option>
    <option value="2">two</option>
    <option value="3">three</option>
</select>

$(document).ready(function (e) {
    var $selects = $('select');
    $selects.change(function () {
        var value = $(this).val();
        if (value == "") {
            return false;
        }
        $selects.not(this).each(function () {
            $('option', this).each(function () {
                var sel = $(this).val();
                if (sel == value) {
                    $(this).prop('disabled', true);
                } else {
                    $(this).prop('disabled', false);
                }
            });
        });
    }).change(); // Trigger once to add options at load of first

});

And a working fiddle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...