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.
The form looks like this:
SIMPLE HTML CODE:
<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>
Now for my 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
}
});
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.
The form looks like this:
SIMPLE HTML CODE:
<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>
Now for my 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
}
});
Share
edited Sep 3, 2017 at 15:19
Ananthakrishnan Baji
1,2901 gold badge9 silver badges21 bronze badges
asked Sep 3, 2017 at 15:09
CecatrixCecatrix
1513 silver badges17 bronze badges
3 Answers
Reset to default 4Hope this may help you 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./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>
If you are using Jquery, the best way to check the value of an input is with the "val()" function. If nothing was selected it will return a value of null.
here is an example :
$("#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./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>
Try a simplified version.
$("#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./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>