尝试获取表单以加载已经检查过先前已检查过的无线电的页面(在新负载时)。
该页面的提交作为php变量保存,我可以将其传递给javascript,其余的应该很简单,但它对我不起作用。
我的代码:
<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>
我的javascript:
$(function() {
var $213 = Yes;
$("input[name='sv_213'][value="$213"]").attr('checked', true);
});?
在上面的js中
var $213 = <dynamically generated content>
这样设置等于已检查的单选按钮的值
这是一个js小提琴,它不起作用: http://jsfiddle.net/CW8AC/
非常感谢您的帮助。
顺便提一句,我的代码基于此前面提到的问题: Set selected radio from radio group with a value
答案 0 :(得分:7)
你需要在你的值“是”附近引用,因为这是一个字符串,而不是布尔值或数字。
var $213 = "Yes";
此外,您需要将变量添加到选择器中,并使用+,如下所示:
$("input[name=sv_213][value="+$213+"]").attr('checked', true);
更新了小提琴,工作:http://jsfiddle.net/CW8AC/1/
完整的js代码:
$(function() {
var $213 = "Yes";
$("input[name=sv_213][value="+$213+"]").attr('checked', true);
});
$("input[name=sv_213][value="+$213+"]").prop('checked', true);
在我的例子中,.attr()方法不能用于动态选择单选按钮,而.prop()则没有。
答案 1 :(得分:0)
在D3中,万一有人想知道。
d3.selectAll("input[name='sv_213'][value=" + $213 + "]").property("checked", true);