javascript 如果单击按钮时未选中复选框,则显示警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16046953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display alert if checkbox isn't checked on button click
提问by user2146226
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?
如何发出弹出警报,以便当用户单击按钮且未选中复选框时,将使用 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>
回答by j08691
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).
您需要捕获按钮的点击事件,然后检查复选框是否被点击。如果没有,您可以发送警报并返回 false(一定要这样做,否则表单将被提交)。
$('#reset').click(function () {
if (!$('#confirm').is(':checked')) {
alert('not checked');
return false;
}
});
回答by Mohammad Adil
$("#reset").on('click',function(){
if(!$("#confirm").is(':checked')){
// alert("");
}
});
回答by bwoebi
$("#reset").click(function(){
if ($("#checkbox:checked").length == 0)
alert("Checkbox not checked!");
});