How to check if a checkbox is checked in jQuery before submitting
<select name="status" id='status'>
<option value="Submitted">Submitted</option>
<option value="Canceled">Canceled</option>
</select>
<input type="checkbox" id="app_box" name="application_complete" value="checked"> App Complete
<script type="text/javascript">
$(function() {
$('form.editable').submit(function(){
if ($('#status').val()=='Canceled') {
if (!confirm('This information will be discarded! )) {
return false;
}
}
});
});
</script>
So, I have the above script that works great. I must add one more confirmation. When the agent clicks the submit button, I want to check if the application checkbox is checked. If it is not checked then display another confirmation field, you need to check the box. How can this be done in jquery.
+3
source to share
4 answers
Like this:
if ($('#app_box').is(':checked')){...}
or
if ($('#app_box')[0].checked){...}
So this is how your code should look like:
$('form.editable').submit(function(){
if (! $('#app_box')[0].checked){
alert('Check App Complete first !');
return false;
}
if ($('#status').val() == 'Canceled') {
if (!confirm('This information will be discarded!' )) {
return false;
}
}
});
More details:
+10
source to share
you can try something like this:
<script type="text/javascript">
$(function() {
$('form.editable').submit(function(){
if ($('#status').val()=='Canceled') {
//check if the box is checked!
if ($("#app_box").is(':checked')) {
alert("You have to check the box 'App Complete'");
return false;
}
if (!confirm('This information will be discarded! )) {
return false;
}
}
});
});
</script>
Hope this helps!
0
source to share