本文介绍了Multiselect,Jquery:检查我的多重选择是否应该选择至少一个选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi I will summarize my problem I hope you would understand.

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

Main Goal: To check if at least one of the options is selected before hitting the select button. else, If select button is clicked without choosing at least one options, it will show a message of "Please choose at least one options".

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

Purpose: I have reasons in my other codes that needed a select button instead of just selecting multiple selects and doesn't need a button.

表单如下所示:

简单的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
  }
});


推荐答案

希望这可以帮到你 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>

这篇关于Multiselect,Jquery:检查我的多重选择是否应该选择至少一个选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:17