如何使用 JavaScript Pure 检查单选按钮?使用表单 id=value
How to check a radio button with JavaScript Pure? Use Form id=value
我需要在 id='A' 的表单中定义一个 Name='status' 和 Value='X' 的单选输入。
就像选择设置一样,在 Value='X' 的表单中,id='B'.
document.querySelector("form[id='A'] input[name='status'][value=1]").checked = true;
document.querySelector("form[id='B'] select[id='cars'][value=volvo]").selected = true;
document.querySelector("form[id='C'] input[name='status'][value=2]").checked = true;
<form id='A'>
FROM A - Radio Test checked: <br>
Value 1 <input type='radio' name='status' value='1' /> <br>
Value 2 <input type='radio' name='status' value='2' /> <br>
Value 3 <input type='radio' name='status' value='3' />
</form>
<hr>
<form id='B'>
FROM B - Select Test selected: <br>
<select id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
</form>
<hr>
<form id='C'>
FROM C - Radio Test checked: <br>
Value 1 <input type='radio' name='status' value='1' /> <br>
Value 2 <input type='radio' name='status' value='2' /> <br>
Value 3 <input type='radio' name='status' value='3' />
</form>
试试这个
document.querySelector("form[id='A'] input[name='status'][value='1']").checked = true;
而不是这个
document.querySelector("form[id='A'] input[name='status'][value=1]").checked = true;
你必须引用价值
我需要在 id='A' 的表单中定义一个 Name='status' 和 Value='X' 的单选输入。 就像选择设置一样,在 Value='X' 的表单中,id='B'.
document.querySelector("form[id='A'] input[name='status'][value=1]").checked = true;
document.querySelector("form[id='B'] select[id='cars'][value=volvo]").selected = true;
document.querySelector("form[id='C'] input[name='status'][value=2]").checked = true;
<form id='A'>
FROM A - Radio Test checked: <br>
Value 1 <input type='radio' name='status' value='1' /> <br>
Value 2 <input type='radio' name='status' value='2' /> <br>
Value 3 <input type='radio' name='status' value='3' />
</form>
<hr>
<form id='B'>
FROM B - Select Test selected: <br>
<select id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
</form>
<hr>
<form id='C'>
FROM C - Radio Test checked: <br>
Value 1 <input type='radio' name='status' value='1' /> <br>
Value 2 <input type='radio' name='status' value='2' /> <br>
Value 3 <input type='radio' name='status' value='3' />
</form>
试试这个
document.querySelector("form[id='A'] input[name='status'][value='1']").checked = true;
而不是这个
document.querySelector("form[id='A'] input[name='status'][value=1]").checked = true;
你必须引用价值