Prevent Multiple Selections of Same Value

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

    Here there is the code to continue selecting and disabling all the times we want.

    First thing is to enable every option, and then look at the selected values, and disable the options which coincides with the selected values.

    These 2 steps are crucial because if you select again, the disabled values of before would continue disabled.

    NEWEST VERSION

    The more elegant way, where we use map() (in stackoverflow there is a good explanation about this method) and filter() jquery functions to do the job. Less lines, and I think same performance or better.

    http://www.jsfiddle.net/dactivo/keDDr/

    $("select").change(function()
     {
    
            $("select option").attr("disabled",""); //enable everything
    
         //collect the values from selected;
         var  arr = $.map
         (
            $("select option:selected"), function(n)
             {
                  return n.value;
              }
          );
    
        //disable elements
        $("select option").filter(function()
        {
    
            return $.inArray($(this).val(),arr)>-1; //if value is in the array of selected values
         }).attr("disabled","disabled");   
    
    });
    

    NEW VERSION

    I have edited my answer, this is my final version:

    http://www.jsfiddle.net/dactivo/kNbWc/

    $("select").change(function()
                       {
    
            $("select option").attr("disabled",""); //enable everything
            DisableOptions(); //disable selected values
    
                       });
    
    
    function DisableOptions()
    {
        var arr=[];
          $("select option:selected").each(function()
                  {
                      arr.push($(this).val());
                  });
    
        $("select option").filter(function()
            {
    
                  return $.inArray($(this).val(),arr)>-1;
       }).attr("disabled","disabled");   
    
    }
    

    OLD VERSION

    http://www.jsfiddle.net/AyxL3/

    $("select").change(function()
                       {
    
            $("select option").attr("disabled",""); //enable everything
            DisableOptions(); //disable selected values
    
                       });
    
    
    function DisableOptions()
    {
    
        $("select option").filter(function()
            {
                  var bSuccess=false; //will be our flag to know it coincides with selected value
                  var selectedEl=$(this);
                  $("select option:selected").each(function()
                  {
    
                      if($(this).val()==selectedEl.val())
                      {
                           bSuccess=true; //it coincides we will return true;
                           return false; // this serves to break the each loop
                       }               
    
                  });
                  return bSuccess;
       }).attr("disabled","disabled");   
    
    }
    
    0 讨论(0)
提交回复
热议问题