问题
How I can issue a pop up alert, such that when a user clicks a button, and a checkbox isn't checked, an alert will appear using jQuery?
The checkbox is:
<input type="checkbox" id="confirm" class="confirm">
The button is:
<form action="resetprocess.php" method="POST">
<button type="submit" id="reset" class="btn btn-danger">Reset <i class="icon-repeat"></i></button>
</form>
回答1:
You need to capture the click event of your button and then check whether the checkbox is clicked or not. If not, you can send the alert and return false (be sure to do this otherwise the form will be submitted anyway).
$('#reset').click(function () {
if (!$('#confirm').is(':checked')) {
alert('not checked');
return false;
}
});
jsFiddle example
回答2:
$("#reset").on('click',function(){
if(!$("#confirm").is(':checked')){
// alert("");
}
});
回答3:
$("#reset").click(function(){
if ($("#checkbox:checked").length == 0)
alert("Checkbox not checked!");
});
来源:https://stackoverflow.com/questions/16046953/display-alert-if-checkbox-isnt-checked-on-button-click