Nam*_*i92 2 javascript input selector radio
嘻,我正在做一些事情,但是可以也不想更改HTML。无法添加ID按钮。我想选择第二个无线电输入。
这是html:
<fieldset>
<legend>I want to sign up:</legend>
<label>
<input type="radio" name="submit-for" value="project">
<span>For project</span>
</label>
<label>
<input type="radio" name="submit-for" value="stage">
<span>As intern</span>
</label>
</fieldset>
Run Code Online (Sandbox Code Playgroud)
这是我尝试在javascript中选择它的方法,但没有成功:
document.querySelector('input[type="radio"]:nth-of-type(2)').onclick = function () {
document.getElementById("project").style.display = 'none';
document.getElementById("stage").style.display = 'block';
};
Run Code Online (Sandbox Code Playgroud)
和
document.querySelector('input[type="radio"]:nth-child(2)').onclick = function () {
document.getElementById("project").style.display = 'none';
document.getElementById("stage").style.display = 'block';
};
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
您是否真的要选择“第二个”单选按钮,还是要选择“值为“ stage”的单选按钮”?第二个选项听起来更可扩展。
document.querySelector('input[type="radio"][value="stage"]')
Run Code Online (Sandbox Code Playgroud)
编辑:此外,您对不求助于ID用法的问题表示赞同。不管您是否被强迫参加,总是一个好的习惯。
| 归档时间: |
|
| 查看次数: |
1404 次 |
| 最近记录: |