[英]Ho to check radio button inside label with javascript
I want to check radio buttons contained in a div based on their parent ID, they set a variable that's used in a function but I can't select them.我想根据父 ID 检查 div 中包含的单选按钮,它们设置了一个在函数中使用的变量,但我无法选择它们。
<label id="radio-step-1" class="btn btn-radio mx-auto" onclick="step=1;">
<input type="radio" autocomplete="off">one
</label>
<label id="radio-step-2" class="btn btn-radio mx-auto" onclick="step=2;">
<input type="radio" autocomplete="off">two
</label>
function setStep(){
document.querySelector("radio-step-" + step "input[type=radio]").checked = true;
}
In your code there is no div
, Do you want to check the label
instead of div.?在您的代码中没有div
,您要检查label
而不是 div。? You have to add #
to select the id, and make the proper concatenation您必须添加#
以选择 ID,并进行适当的连接
document.querySelector("#radio-step-" + step+" input[type=radio]").checked = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.