Multiselect,Jquery:检查我的多重选择是否应该选择至少一个选项

时间:2017-09-03 15:09:13

标签: javascript jquery multi-select

您好我会总结一下我的问题,希望您能理解。

主要目标:要在选择按钮之前检查是否至少选择了其中一个选项。否则,如果单击选择按钮而未选择至少一个选项,则会显示一条消息"请选择至少一个选项"。

目的:我的其他代码中有理由需要选择按钮,而不是只选择多个选项而不需要按钮。

表格如下:

enter image description here

简单的HTML代码:

<div class="form-group">
  // My Select Id is monthlymonth

  <label for="monthlymonth" class="col-sm-4 control-label">For the Month Of: </label>
  <div class="col-sm-6" id=monthlymonthdiv>
      <select class="form-control" name="monthlymonth[]" id="monthlymonth" multiple>
          <option> October </option>
          <option> November </option>
          <option> December </option>
      </select>
   </div>

   /My Select button, Id is monthsubmit.
   <input style="text-align:center; width: 60px;" type="button" id="monthsubmit" name="monthsubmit" value="Select">

   <input style="text-align:center; width: 60px;" type="button" id="clear" name="clear" value="Clear">
</div>

现在我的JQuery:

$("#monthsubmit").unbind('click').bind('click', function() {
  if (// code that checks if no options is selected at least one)
  {
    alert('Please choose at least one month');
  }
  else
  {
    // my other codes
  }
});

3 个答案:

答案 0 :(得分:1)

希望这可以帮助你if ($('#monthlymonth').get(0).selectedIndex == -1)

$("#monthsubmit").on('click', function() {
			  if ($('#monthlymonth').get(0).selectedIndex == -1)
			  {
			    alert('Please choose at least one month');
			  }
			  else
			  {
			    // my other codes
			  }
			});

$("#clear").on('click', function() {
				$("#monthlymonth option:selected").removeAttr("selected");
			});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">

  <label for="monthlymonth" class="col-sm-4 control-label">For the Month Of: </label>
  <div class="col-sm-6" id=monthlymonthdiv>
      <select class="form-control" name="monthlymonth[]" id="monthlymonth" multiple>
          <option> October </option>
          <option> November </option>
          <option> December </option>
      </select>
   </div>

   <input style="text-align:center; width: 60px;" type="button" id="monthsubmit" name="monthsubmit" value="Select">

   <input style="text-align:center; width: 60px;" type="button" id="clear" name="clear" value="Clear">
</div>

答案 1 :(得分:1)

如果您使用的是Jquery,检查输入值的最佳方法是使用“val()”函数。 如果未选择任何内容,则返回null值。

这是一个例子:

$("#monthsubmit").unbind('click').bind('click', function() {
  if (!$("#monthlymonthdiv").val())
  {
    alert('Please choose at least one month');
  }
  else
  {
    // my other codes
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">

  <label for="monthlymonth" class="col-sm-4 control-label">For the Month Of: </label>
  <div class="col-sm-6" id=monthlymonthdiv>
      <select class="form-control" name="monthlymonth[]" id="monthlymonth" multiple>
          <option> October </option>
          <option> November </option>
          <option> December </option>
      </select>
   </div>

   <input style="text-align:center; width: 60px;" type="button" id="monthsubmit" name="monthsubmit" value="Select">

   <input style="text-align:center; width: 60px;" type="button" id="clear" name="clear" value="Clear">
</div>

答案 2 :(得分:1)

试用简化版。

$("#monthsubmit").click(function(){
    if (!$("#monthlymonth option:selected").length) {
        alert('Please choose at least one month');
    }
});

$("#clear").click(function(){
    $("#monthlymonth option:selected").removeAttr("selected");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">

  <label for="monthlymonth" class="col-sm-4 control-label">For the Month Of: </label>
  <div class="col-sm-6" id=monthlymonthdiv>
      <select class="form-control" name="monthlymonth[]" id="monthlymonth" multiple>
          <option> October </option>
          <option> November </option>
          <option> December </option>
      </select>
   </div>

   <input style="text-align:center; width: 60px;" type="button" id="monthsubmit" name="monthsubmit" value="Select">

   <input style="text-align:center; width: 60px;" type="button" id="clear" name="clear" value="Clear">
</div>