小编Nam*_*i92的帖子

如何在不使用ID的情况下在javascript中选择第二个单选按钮?

嘻,我正在做一些事情,但是可以也不想更改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)

有人可以帮忙吗?

javascript input selector radio

2
推荐指数
1
解决办法
1404
查看次数

标签 统计

input ×1

javascript ×1

radio ×1

selector ×1