Prevent Multiple Selections of Same Value

后端 未结 7 755
渐次进展
渐次进展 2020-12-09 06:36

I am working on a project where I have a form that will have multiple \'select\' inputs, all with the same set of options. I would like to use jquery to disable/hide an opt

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 07:22

    Here's a working example:

    http://jsfiddle.net/aNxBy/

    With your data, you have to do this:

    $(document).ready(function() {
        $('select').change(function() {
            $('option[value=' + $(this).val() + ']').attr('disabled', 'disabled');
        });
    });
    

    Be mindful, however, of the fact that this won't disable the disable after you've changed it to another value. I didn't add this in here because you didn't specify exactly what you needed to do after you disabled the option...really it could be a number of possible scenarios.

    One thing you could do is to cycle through all relevant select elements when the change event fires, find the values of all the selected options, and disable where appropriate.

提交回复
热议问题