如果在按钮点击时未选中复选框,则显示警报 [英] Display alert if checkbox isn't checked on button click

查看:688
本文介绍了如果在按钮点击时未选中复选框,则显示警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何发布弹出式快讯,当使用者点击按钮,并且未勾选核取方块时,会使用jQuery显示快讯?

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?

该复选框是:

<input type="checkbox" id="confirm" class="confirm">

按钮是:

<form action="resetprocess.php" method="POST">
  <button type="submit" id="reset" class="btn btn-danger">Reset <i class="icon-repeat"></i></button> 
</form>


推荐答案

您需要捕获按钮的点击事件,然后检查该复选框是否被点击。如果没有,您可以发送警报并返回false(一定要这样做,否则将提交表单)。

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示例

jsFiddle example

这篇关于如果在按钮点击时未选中复选框,则显示警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆