html单选按钮禁用,使用jQuery禁用单选按钮

使用jQuery禁用单选按钮

我试图在单击loadActive链接时禁用这些单选按钮,但由于某种原因,它仅禁用顺序中的第一个按钮,然后跳过其余的按钮。

Load Active

这是我正在使用的jQuery:

jQuery("#loadActive").click(function() {

//I have other code in here that runs before this function call

writeData();

});

function writeData() {

jQuery("input[name='ticketID']").each(function(i) {

jQuery(this).attr('disabled', 'disabled');

});

}

user46785 asked 2020-07-29T22:02:22Z

7个解决方案

47 votes

我对您的代码进行了一些重构,这应该可以工作:

jQuery("#loadActive").click(writeData);

function writeData() {

jQuery("#chatTickets input:radio").attr('disabled',true);

}

如果表单上有两个以上的单选按钮,则必须修改选择器,例如,可以使用“开始于”属性过滤器来选择ID以ticketID开头的单选:

function writeData() {

jQuery("#chatTickets input[id^=ticketID]:radio").attr('disabled',true);

}

karim79 answered 2020-07-29T22:02:43Z

42 votes

删除您的“每个”,然后使用:

$('input[name=ticketID]').attr("disabled",true);

这么简单。 有用

KCOtzen answered 2020-07-29T22:03:07Z

9 votes

首先,有效语法为

jQuery("input[name=ticketID]")

第二,您是否尝试过:

jQuery(":radio")

代替?

第三,为什么不为所有单选按钮分配一个班级,然后按班级选择它们?

mkoryak answered 2020-07-29T22:03:40Z

5 votes

只需使用jQuery prop

$(".radio-button").prop("disabled", false);

$(".radio-button").prop("disabled", true); // disable

mostafaznv answered 2020-07-29T22:04:00Z

3 votes

我刚刚用您的代码构建了一个沙箱环境,它对我有用。 这是我使用的:

test

Load Active

jQuery("#loadActive").click(function() {

//I have other code in here that runs before this function call

writeData();

});

function writeData() {

jQuery("input[name='ticketID']").each(function(i) {

jQuery(this).attr('disabled', 'disabled');

});

}

我在FF3.5中进行了测试,现在移至IE8。 而且它在IE8中也可以正常工作。 你使用的是什么浏览器?

geowa4 answered 2020-07-29T22:04:24Z

2 votes

码:

function writeData() {

jQuery("#chatTickets input:radio[id^=ticketID]:first").attr('disabled', true);

return false;

}

另请参阅:选择器/收音机,选择器/ attributeStartsWith,选择器/第一个

ranonE answered 2020-07-29T22:04:48Z

1 votes

您应该使用类使其更简单,而不是使用属性名称或任何其他jQuery选择器。

rogeriopvl answered 2020-07-29T22:05:08Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值