使用jQuery禁用单选按钮
我试图在单击loadActive链接时禁用这些单选按钮,但由于某种原因,它仅禁用顺序中的第一个按钮,然后跳过其余的按钮。
这是我正在使用的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
我刚刚用您的代码构建了一个沙箱环境,它对我有用。 这是我使用的:
testjQuery("#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