如何使用jQuery从multiselect选项中取消选择值?

时间:2016-09-01 08:04:20

标签: javascript jquery html dom html-select

我有一个多选项。如果我选择Mahesh& Dilip两个,我想显示一个弹出消息,你不能一次选择两个。如果我在弹出时点击确定,那么我想取消选择这两个。这该怎么做?这是我的代码:

<select multiple="" name="playerNames" id="playerNames" class=""> 
   <option value="">-- Select --</option> 
   <option value="4">Rakesh</option> 
   <option value="5">Suresh</option> 
   <option value="2">Mahesh</option> 
   <option value="6">Dilip</option> 
   <option value="1">Ramesh</option> 
   <option value="3">Dinesh</option> 
</select>

<script type="text/javascript">
   $('#playerNames').on('change',function(){  
   var selected = $('#playerNames:selected').map(function(){return $(this).val();}).get();
   alert(selected.length);   
   if(jQuery.inArray("Mahesh", selected) !== -1){
     var maheshSelected = true;
   }

   if(jQuery.inArray("Dilip", selected) !== -1){
      var dilipSelected = true;
   }

   if(maheshSelected == true && dilipSelected == true){
      var alertMessage = "You cannot choose Mahesh + Dilip. Please select either Mahesh or Dilip.";
    alert(alertMessage);
    if (confirm(alertMessage)) {

    //code to deselect  both

    }
  }

});

1 个答案:

答案 0 :(得分:1)

您可以使用$('option:contains(Mahesh), option:contains(Dilip)')选择选项,并使用.prop('selected', false)取消选择:

&#13;
&#13;
$('#playerNames').on('change', function(){  
  const selected = $('#playerNames :selected').map(function() {return $(this).text()}).get()
  if (selected.includes('Mahesh') && selected.includes('Dilip')) {
    alert('You cannot choose Mahesh + Dilip. Please select either Mahesh or Dilip.')
    $('option:contains(Mahesh), option:contains(Dilip)').prop('selected', false)
  }
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple="" name="playerNames" id="playerNames" class=""> 
  <option value="">-- Select --</option> 
  <option value="4">Rakesh</option> 
  <option value="5">Suresh</option> 
  <option value="2">Mahesh</option> 
  <option value="6">Dilip</option> 
  <option value="1">Ramesh</option> 
  <option value="3">Dinesh</option> 
</select>
&#13;
&#13;
&#13;

这是一个跨浏览器的解决方案(适用于Internet Explorer 9 +):

&#13;
&#13;
$('#playerNames').on('change', function(){  
  var selected = $('#playerNames :selected').map(function() {return $(this).text()}).get()
  if (selected.indexOf('Mahesh') > -1 && selected.indexOf('Dilip') > -1) {
    alert('You cannot choose Mahesh + Dilip. Please select either Mahesh or Dilip.')
    $('option:contains(Mahesh), option:contains(Dilip)').prop('selected', false)
  }
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple="" name="playerNames" id="playerNames" class=""> 
  <option value="">-- Select --</option> 
  <option value="4">Rakesh</option> 
  <option value="5">Suresh</option> 
  <option value="2">Mahesh</option> 
  <option value="6">Dilip</option> 
  <option value="1">Ramesh</option> 
  <option value="3">Dinesh</option> 
</select>
&#13;
&#13;
&#13;