[英]Dynamically checked radio button, based on 'value' attribute, not checking
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 $213 = Yes;
$("input[name='sv_213'][value="$213"]").attr('checked', true);
});?
In the above js the 在上面的js中
var $213 = <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 顺便提一下,我的代码基于此前面提到的问题: 使用值设置来自无线电组的选定无线电
You need quotes around your value "Yes", since this is a string, and not a boolean or number. 您需要围绕值“是”的引号,因为这是一个字符串,而不是布尔值或数字。
var $213 = "Yes";
Also, you need to add the variable into the selector with +'s like this: 此外,您需要将变量添加到选择器中,使用+,如下所示:
$("input[name=sv_213][value="+$213+"]").attr('checked', true);
Updated fiddle, working: http://jsfiddle.net/CW8AC/1/ 更新小提琴,工作: http : //jsfiddle.net/CW8AC/1/
Full js code: 完整的js代码:
$(function() {
var $213 = "Yes";
$("input[name=sv_213][value="+$213+"]").attr('checked', true);
});
$("input[name=sv_213][value="+$213+"]").prop('checked', true);
in my case, the .attr() method didnt worked for dynamically selecting the radio button whereas .prop() did. 在我的例子中,.attr()方法没有用于动态选择单选按钮而.prop()没有。
在D3中,万一有人在想。
d3.selectAll("input[name='sv_213'][value=" + $213 + "]").property("checked", true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.