jQuery 根据值禁用单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7454224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:52:12  来源:igfitidea点击:

Disable radio buttons based on value

jqueryradio

提问by Ajay

I want to disable radio buttons based on the value of a variable. The radio that is equal to the variable value should be disabled.

我想根据变量的值禁用单选按钮。应禁用等于变量值的无线电。

Ex:

前任:

<input type="radio" name="r1" value="a" />Value a
<input type="radio" name="r1" value="b" />Value b

So if the $variable = 'a';then the radio button which has the value ashould be disabled.

因此,如果则应禁用$variable = 'a';具有该值的单选按钮a

回答by ipr101

Try -

尝试 -

var a = 'a';
$("input[type=radio][value=" + a + "]").prop("disabled",true);

or

或者

var a = 'a';
$("input[type=radio][value=" + a + "]").attr("disabled",true);

If you're using an older jQuery version.

如果您使用的是较旧的 jQuery 版本。

Working demo - http://jsfiddle.net/FtwcL/1

工作演示 - http://jsfiddle.net/FtwcL/1

回答by sbarrat

If you want to disable based by name and value, you can try this

如果你想禁用基于名称和值,你可以试试这个

var a = 'a';
var fieldName = 'r1';
$('input[type=radio][name=' + fieldName + '][value='+ a +']').prop('disabled', true);