Prevent Multiple Selections of Same Value

后端 未结 7 729
渐次进展
渐次进展 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:20

    To hide them, use the following approach (since IE bug prevents using CSS "display" property setting to "none" on an OPTION):

    -Store the original list of options in an array

    -Have a function to build the select #x from an array, slipping previously selected items

    -Assign an onchange handler for all selects which loops through all later selects and calls this function.

    var option_value_order = {'volvo', 'saab', 'mercedes'};
    var option_display_strings = new Array();
    option_display_strings['volvo'] = 'Volvo'; 
    //...
    
    // Assume each of the selects' ID value is "select_1", "select_2" instead of "1", "2"
    function redraw(selectNum) {
        var selectId = "select_" + selectNum;
        var s = document.getElementById(selectId);
        s.options.length = 0; // empty out
        var next = 0;
        for (var i = 0; i < option_value_order.length; i++) { 
            var skip = 0;
            for (var select_index = 0; select_index < selectNum; select_index++) {
                if (document.getElementById("select_"+select_index).selected == i)
                    skip = 1;
                if (skip == 0) {
                    var o = option_value_order[i];
                    s.options[next] = o;
                    // Don't recall how to set value different from option display, 
                    // using option_display_strings[o] 
                    next++;
                }
            }
        }
    }
    
    var total_selects = 2;
    
    function change_later_selects(s) {
        var select_num = find_number(s.id); "this returns number 2 for "select_2" - homework
        for (var i = select_num + 1; i <= total_selects; i++) {
            redraw(i);
        }
    }
    

    And then the HTML

    
    

提交回复
热议问题