Javascript 如何为单选按钮和复选框设置禁用/只读功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10583392/
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-24 01:58:28  来源:igfitidea点击:

How to make disabled/readonly functionality for radio buttons and checkboxes

javascriptjqueryhtmlcss

提问by Raje

I know we can't make readonlyradio button and checkbox. I tried to disable radio button and checkbox but in IE it is not visible.

我知道我们不能制作只读单选按钮和复选框。我试图禁用单选按钮和复选框,但在 IE 中它不可见。

Is any alternative way to do the same like disabled/ readonlyusing jQuery, CSS, JavaScript?

是否有任何替代方法可以使用 jQuery、CSS、JavaScript禁用/只读

回答by LoneWOLFs

You can use the following code to avoid clicks on elements you don't want user to change

您可以使用以下代码来避免点击您不希望用户更改的元素

$(':radio,:checkbox').click(function(){
    return false;
});

You can use proper selector to limit the clicks.

您可以使用适当的选择器来限制点击次数。

回答by Javascript Coder

Yon can write like this:-

Yon 可以这样写:-

in your script:

在你的脚本中:

 jQuery('#radio').attr('disabled','disabled');

or you can add class according to you..

或者你可以根据你添加类..

if it will not compatible with IE give compatibility also..

如果它与 IE 不兼容也提供兼容性..

<meta http-equiv="X-UA-Compatible" content="IE=8" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />

then check..

然后检查..

you can also try this:

你也可以试试这个:

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

回答by driven4ward

selectedRow
    .find('input[type="radio"]:not(:checked)')
    .each(function(){
        $(this).attr('disabled', true);
});

selected row is just the HTML element below which I want to locate the radio buttons I am interested.
the whole script is only called once a radio button has been selected.
when executed, it locates the unselected radio buttons and disables them.

selected row 只是下面的 HTML 元素,我想在它下面找到我感兴趣的单选按钮。
只有在选择了单选按钮后才会调用整个脚本。
执行时,它会定位未选中的单选按钮并禁用它们。

hence, giving me the desired result of disabling the non-selected radio buttons, while maintaining the selected option.

因此,给了我禁用未选择的单选按钮的期望结果,同时保持选定的选项。

this result seemed to be a better option, then
1. laying an image over the radio button, to avoid the user from selected another option.
2. .click(function(){ return false; }), which just hides the selection when the unselected option is selected 3. disabling all the radio buttons, where the selection is lost.

这个结果似乎是一个更好的选择,然后
1. 在单选按钮上放置一个图像,以避免用户选择另一个选项。
2. .click(function(){ return false; }),当未选择的选项被选中时,它只是隐藏选择 3. 禁用所有单选按钮,其中选择丢失。

hope this helps you,
cheers.

希望这对你有帮助,
干杯。

回答by Raje

If radio button is already checked and if you click on checked radio button it change value from true to false. For this scenario following code works

如果单选按钮已被选中,并且如果您单击选中的单选按钮,它将值从 true 更改为 false。对于这种情况,以下代码有效

$(document).ready(function () {
    $('input[type = "radio"]').change(function () {
        this.checked = false;
    });
});