禁用按钮,直到单击一个单选按钮 [英] Disable button until one radio button is clicked

查看:62
本文介绍了禁用按钮,直到单击一个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正忙于注册,我希望人们选择性别。我通过使用单选按钮来完成此操作。现在,我想要的是,有一个禁用的帖子按钮,直到两个框中的一个被选中,这是我用jQuery做的:

So I'm busy on a registration, and I want people to choose their gender. I do this by the use of radio buttons. Now, what I want, is to have a disabled post button untill one of the two boxes is selected, this I do with jQuery:

var $radio = $("input:radio");
$radio.change(function() 
{
    var checkedButtons = false;
    $radio.each(function() {
        if (this.checked) 
        {
            var checkedButtons = true;
            return false;
        }
    });
if (checkedButtons)
{
    $("#postGender").removeAttr("disabled");
}
    else
{
    $("#postGender").attr("disabled", "disabled");
}

});

这个小块是代码,是我在stackoverflow上找到的。唯一不对的是它不起作用。

This little piece is code, was found by me on stackoverflow. The only thing wrong is that it doesn't work.

有关更多代码和演示,请参阅此内容: JsFiddle

See this for more code and ofcouse a demo: JsFiddle

推荐答案

您可以将所有内容减少到一行:

You could reduce all that to one line:

$("input:radio").change(function () {$("#postGender").prop("disabled", false);});

jsFiddle示例

jsFiddle example

这篇关于禁用按钮,直到单击一个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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