Javascript 访问单选按钮值
Html代码:-Javascript 访问单选按钮值,javascript,Javascript,Html代码:- <input type="radio" name="radio" id="radio" value="1" onclick="validate()"> For Yourself</input> </p></br> <p> <input type="radio" name="radio" id="radio" value="2" onclick="validate()"> For Users&
<input type="radio" name="radio" id="radio" value="1" onclick="validate()"> For Yourself</input> </p></br>
<p>
<input type="radio" name="radio" id="radio" value="2" onclick="validate()"> For Users</input>
</p>
现在,每当我试图打印单选按钮的值时。它总是将值打印为1
所以,现在我不明白我到底错过了什么
谢谢你的帮助。试试这个
<input type="radio" name="radio" id="radio" value="1" onclick="validate(this)"> For Yourself</input> </p></br>
<p>
<input type="radio" name="radio" id="radio" value="2" onclick="validate(this)"> For Users</input>
</p>
function validate(ele)
{
alert(ele.value);
}
为你自己
对于用户
功能验证(ele)
{
警报(元素值);
}
试试这个
<input type="radio" name="radio" id="radio" value="1" onclick="validate(this)"> For Yourself</input> </p></br>
<p>
<input type="radio" name="radio" id="radio" value="2" onclick="validate(this)"> For Users</input>
</p>
function validate(ele)
{
alert(ele.value);
}
为你自己
对于用户
功能验证(ele)
{
警报(元素值);
}
元素ID
应该是唯一的。我修改了你的HTML和JS部分,并在下面检查
试试这个
HTML
<p>
<input type="radio" name="radio" id="radio1" value="1" onclick="validate(this)"> For Yourself</input> </p></br>
<p>
<input type="radio" name="radio" id="radio2" value="2" onclick="validate(this)"> For Users</input>
</p>
元素
ID
应该是唯一的。我修改了你的HTML和JS部分,并在下面检查
试试这个
HTML
<p>
<input type="radio" name="radio" id="radio1" value="1" onclick="validate(this)"> For Yourself</input> </p></br>
<p>
<input type="radio" name="radio" id="radio2" value="2" onclick="validate(this)"> For Users</input>
</p>
首先,不要在html中两次使用DOMID! 移除它们。。。。仅使用重复的名称
<input type="radio" name="radio" value="1" onclick="validate()"> For Yourself</input> </p></br>
<p>
<input type="radio" name="radio" value="2" onclick="validate()"> For Users</input>
</p>
首先,不要在html中两次使用DOMID! 移除它们。。。。仅使用重复的名称
<input type="radio" name="radio" value="1" onclick="validate()"> For Yourself</input> </p></br>
<p>
<input type="radio" name="radio" value="2" onclick="validate()"> For Users</input>
</p>
您只能有一个具有特定ID的元素。
文档。getElementById
通常会获取文档顺序中的第一个元素,尽管此行为未定义。您只能有一个具有特定ID的元素。文档。getElementById
通常会获取文档顺序中的第一个元素,尽管此行为未定义。
<input type="radio" name="radio" value="1" onclick="validate(this)"> For Yourself</input> </p>
function validate(domElement) {
if(domElement.checked === true) {
alert(elements[n].value);
}
}