【发布时间】:2020-11-23 11:39:28
【问题描述】:
我想使用 jQuery 选择 :nth-of-type 单选按钮,使用整数变量作为参数。但我的 jquery 代码不起作用。
这是我的html示例:
<div class="col-md-12 question" id="q1">
<span class="q">1) What about shoelaces and long clothing?</span><br>
<span class="parent"><input type="radio" name="q1" value="0" class="a"> I don’t need to care, the escalator is completely closed</span><br>
<span class="parent"><input type="radio" name="q1" value="1" class="a"> <span class="cor">I tighten my shoelaces and keep clothing away</span></span><br>
<span class="parent"><input type="radio" name="q1" value="0" class="a"> I shouldn’t wear shoes on an escalator</span><br>
<span class="parent"><input type="radio" name="q1" value="0" class="a"> It’s a challenge to see if the escalator can catch my shoelaces</span><br><br>
<p class="info">You should check your laces and long loose clothes before you step on. Tie your laces before you get on and keep long clothes away from the steps and handrail to avoid that they get mechanically couth between steps or underneath the handrail.</p>
这是我的 jQuery 代码:
// get value from url parameter (integer)
var q1 = urlParams.get('q1');
// check :nth radio button
$("#q1 .a:nth-of-type(" + q1 + ")").prop("checked", true);
这似乎不起作用。我也试过 .attr("checked", true) 和 .prop("checked", "checked")。两者都不工作。有人可以帮忙吗?
提前致谢。
【问题讨论】:
-
你想得到什么?你的问题不清楚?
-
你在第一季度得到的价值是多少?某个整数值?
-
@maverickosama92 确实是一个整数
-
像这样使用它 $("#q1 .a").eq(q1).prop("checked", true);
-
是的!正如@maverickosama92 建议你需要使用
$("#q1 .a").eq(q1).prop("checked", true);
标签: jquery input radio-button attr prop