选择onselect all禁用其他值 - PHP和jQuery

时间:2015-10-28 11:06:45

标签: php jquery html

我的项目中有selectmultiple选项。代码在

之下
<select name='sendto[]' id='sendto' multiple required>
   <option value='branchstaff'>Branch Staff</option>
   <option value='manager'>Manager</option>
   <option value='cashier'>Cashier</option>
   <option value='hostaff'>Head Office Staff</option>
   <option value='all'>All of Above</option>
</select>

我在这个项目中使用jQuery。现在我想选择一些值来发送消息。单击All of Above

时,如何取消选择所有其他值

4 个答案:

答案 0 :(得分:1)

我就是这样做的:

$("option").click(function () {
    if ($(this).text() == "All of Above") {
        $(this).siblings().removeAttr("selected");
    }
});

Here is the JSFiddle demo

答案 1 :(得分:0)

$('option[value="all"]').on('click',function(){
  $(this).parent().find('option').attr('selected','selected');
  $(this).removeAttr('selected');
});

答案 2 :(得分:0)

$('#sendto').change(function() {
  alert($('option:selected', this).val())
  if ($('option:selected', this).val() === 'all') {
    $("select[multiple] option").prop("selected", "selected");
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select name='sendto[]' id='sendto' multiple required>
  <option value='branchstaff'>Branch Staff</option>
  <option value='manager'>Manager</option>
  <option value='cashier'>Cashier</option>
  <option value='hostaff'>Head Office Staff</option>
  <option value='all'>All of Above</option>
</select>

    $('#sendto').change(function () {
    if ($('option:selected', this).val() == 'all') {
        $("select[multiple] option").prop("selected", "selected");
    }
})

<select name='sendto[]' id='sendto' multiple required>
   <option value='branchstaff'>Branch Staff</option>
   <option value='manager'>Manager</option>
   <option value='cashier'>Cashier</option>
   <option value='hostaff'>Head Office Staff</option>
   <option value='all'>All of Above</option>
</select>

试试这个

答案 3 :(得分:0)

尝试以下

$('#sendto option[value="all"]').on('click',function(){
		  $('#sendto').find('option').attr('selected','selected');
		});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name='sendto[]' id='sendto' multiple required>
   <option value='branchstaff'>Branch Staff</option>
   <option value='manager'>Manager</option>
   <option value='cashier'>Cashier</option>
   <option value='hostaff'>Head Office Staff</option>
   <option value='all'>All of Above</option>
</select>