jQuery: выберите радиокнопку: nth-of-type (целочисленная переменная) - PullRequest
0 голосов
/ 03 августа 2020

Я хочу выбрать радиокнопку: nth-of-type с jQuery, используя целочисленную переменную в качестве параметра. Но мой 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 («проверено», верно) и .prop («проверено», «проверено») . Оба тоже не работают. Может кто поможет?

Заранее спасибо.

1 Ответ

0 голосов
/ 03 августа 2020

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>
</div>

JS:

// get value from url parameter
var q1 = 1; //urlParams.get('q1');

// check :nth radio button
$("#q1 .a").eq(q1).prop("checked", true);

Рабочий JSFiddle:

https://jsfiddle.net/h8wfuz31/

...