Javascript 动态检查的单选按钮,基于“值”属性,不检查

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

Dynamically checked radio button, based on 'value' attribute, not checking

javascripthtmlformsradio-button

提问by Gideon

Trying to get a form to load pages with the previously checked radios already checked (upon a fresh load).

试图获取一个表单来加载先前检查过的无线电已经检查过的页面(在新加载时)。

The submissions of that page are held as php variables which I can pass into the javascript, the rest should be simple, but it's not working for me.

该页面的提交作为 php 变量保存,我可以将其传递给 javascript,其余的应该很简单,但它对我不起作用。

My code:

我的代码:

<div class="question">
            <label for="sv_213">Question?</label> 
            <input 
                    class="radio"
                    type="radio" 
                    value="Yes" 
                    name="sv_213" 
                />Yes
            <input 
                    class="radio"
                    type="radio" 
                    value="No" 
                    name="sv_213"
                />No

        </div>

My javascript:

我的javascript:

$(function() {
    var 3 = Yes;
    $("input[name='sv_213'][value="3"]").attr('checked', true);

    });?

In the above js the

在上面的js中

var 3 = <dynamically generated content>

Such that it is set equal to the value of the radio button that was checked

这样它就被设置为等于被选中的单选按钮的值

Here is a js fiddle with it not working: http://jsfiddle.net/CW8AC/

这是一个 js 小提琴,它不起作用:http: //jsfiddle.net/CW8AC/

Many thanks for any help.

非常感谢您的帮助。

Incidentally my code is based on this, previously asked question: Set selected radio from radio group with a value

顺便说一句,我的代码基于此,先前提出的问题: Set selected radio from radio group with a value

回答by keithwyland

You need quotes around your value "Yes", since this is a string, and not a boolean or number.

您的值“是”需要引号,因为这是一个字符串,而不是布尔值或数字。

var 3 = "Yes";

Also, you need to add the variable into the selector with +'s like this:

此外,您需要使用 + 将变量添加到选择器中,如下所示:

$("input[name=sv_213][value="+3+"]").attr('checked', true);

Updated fiddle, working: http://jsfiddle.net/CW8AC/1/

更新小提琴,工作:http: //jsfiddle.net/CW8AC/1/

Full js code:

完整的js代码:

$(function() {
    var 3 = "Yes";
    $("input[name=sv_213][value="+3+"]").attr('checked', true);

    });

Update:## Since jQuery 1.6, you can also use the .prop method with a boolean value (this should be the preferred method):

更新:## 从 jQuery 1.6 开始,您还可以使用带有布尔值的 .prop 方法(这应该是首选方法):

$("input[name=sv_213][value="+3+"]").prop('checked', true);

in my case, the .attr() method didnt worked for dynamically selecting the radio button whereas .prop() did.

在我的例子中, .attr() 方法不能用于动态选择单选按钮,而 .prop() 可以。

回答by Harry Stevens

And in D3, in case anyone was wondering.

在 D3 中,以防万一有人想知道。

d3.selectAll("input[name='sv_213'][value=" + 3 + "]").property("checked", true);