当按钮被禁用时,JQuery注册点击事件 [英] JQuery register click event while button is disabled

查看:195
本文介绍了当按钮被禁用时,JQuery注册点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已禁用的输入按钮,将在选中复选框时启用。
现在,我想要的是,当按钮被点击时,按钮显示一个警告,而禁用通知用户他需要首先检查复选框。

I have a disabled input button, that will get enabled when checking a checkbox. Now, what I want is that the button shows an alert when it is clicked while is disabled to inform the user that he needs to check the checkbox first.

<input type="submit" disabled="disabled" id="proceedButton">

当我点击按钮时,没有任何反应,因为它被禁用

When I click the button nothing happens because it is disabled

$("input#proceedButton").click(function() {
    if (!$("input#acceptCheckbox").is(':checked')) {
        alert("show me");
    }
});


推荐答案

您无法禁用此按钮,单击该按钮时将选中该复选框。如果从事件处理程序返回 false ,则会阻止该按钮的默认操作,因此它被有效禁用,但仍可点击:

You could not disable the button, and instead check whether the checkbox is checked when the button is clicked. If you return false from the event handler the default action of the button will be prevented, so it is effectively disabled, but still clickable:

$("input#proceedButton").click(function() {
    if (!$("input#acceptCheckbox").is(':checked')) {
        alert("show me");
        return false; //Prevent the default button action
    }
});

注意,由于 id 必须是唯一的,您应该能够安全地忽略您的选择器的输入部分,只需使用 id 选择器。

As a side note, since id values have to be unique you should be able to safely omit the input part of your selectors and just use the id selector.

这篇关于当按钮被禁用时,JQuery注册点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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