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
877 views
in Technique[技术] by (71.8m points)

javascript - Selecting the last value from the dropdown and disable it for other dropdowns

I have 3 drop-downs with values: Select, One, Two, Three and following is the HTML.

<select class="dpdown" id="box1">
    <option value="Select">Select</option>
    <option value="One">One</option>
    <option value="Two">Tow</option>
    <option value="Three">Three</option>
</select> 
<select class="dpdown" id="box2">
    <option value="Select">Select</option>
    <option value="One">One</option>
    <option value="Two">Tow</option>
    <option value="Three">Three</option>
</select> 
<select class="dpdown" id="box3">
    <option value="Select">Select</option>
    <option value="One">One</option>
    <option value="Two">Tow</option>
    <option value="Three">Three</option>
</select> 

Conditions

  1. First of all the jQuery function should store the current values from all the boxes.
  2. If the user select One from #box1, then it (One) should be disabled from all other boxes, and if Two is selected from #box1 again, then Two is disabled from other boxes and One gets enabled. i.e. the last value is disabled not all the values.
  3. Also to be noted that the user can select from any of the dropdowns whether it is box1/box2 or box3.

EDIT

$(document).ready(function(){
    $('#box1').data('pre', $(this).val()); // added this line to get the pre value.
    $("select").change(function(e){
        var id = $(this).attr("id");
        var before_change = $(this).data('pre');
        alert(before_change); // alert shows blank values
    });
});

I want to do this with jQuery. Please help.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Try something like this:

var $selects = $('.dpdown').change(function() {
    var val = $(this).val();
    $selects
        .children().prop('disabled', false)
        .end().not(this)
        .find('[value="' + val + '"]').prop('disabled', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="dpdown" id="box1">
    <option value="Select">Select</option>
    <option value="One">One</option>
    <option value="Two">Two</option>
    <option value="Three">Three</option>
</select> 
<select class="dpdown" id="box2">
    <option value="Select">Select</option>
    <option value="One">One</option>
    <option value="Two">Two</option>
    <option value="Three">Three</option>
</select> 
<select class="dpdown" id="box3">
    <option value="Select">Select</option>
    <option value="One">One</option>
    <option value="Two">Two</option>
    <option value="Three">Three</option>
</select>

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

...