您好,我将总结我的问题,希望您能理解。
主要目标:在点击选择按钮之前检查是否至少选择了一个选项。否则,如果在没有选择至少一个选项的情况下单击选择按钮,则会显示一条消息“请选择至少一个选项”。
Purpose:我的其他代码中有一些原因需要一个选择按钮,而不是只选择多个选择,并且不需要按钮。
表格如下所示:
简单的 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)