使用 javascript 访问单选按钮状态
我使用 struts2 创建了一个单选按钮小部件。我想使用 javascript 函数检索选定的单选按钮值。请帮我。下面的代码仅检索第一个元素。
function getAddress()
{
var add=document.getElementById("add").value;
alert(add);
window.opener.setAddress(add);
window.close();
return true;
}
</script>
<body>
<s:action name="Address" id="addlist"/>
<s:radio label="Address" name="add" list="employeeDetail.addlist">
<br>
</s:radio>
<input type="button" name="nextButton" id="nextButton" value="Select"
onclick="javascript:getAddress();" />
</body>
</html>
I have created a radio button widget using struts2. I want to retrieve the selected radio button value using javascript function. Please help me. The below code retrieves only the first element.
function getAddress()
{
var add=document.getElementById("add").value;
alert(add);
window.opener.setAddress(add);
window.close();
return true;
}
</script>
<body>
<s:action name="Address" id="addlist"/>
<s:radio label="Address" name="add" list="employeeDetail.addlist">
<br>
</s:radio>
<input type="button" name="nextButton" id="nextButton" value="Select"
onclick="javascript:getAddress();" />
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已将单选按钮名称指定为“add”,但在 javascript 中访问时,您尝试通过 id 访问它,即将
document.getElementById("add")
更改为document.getElementByName( “添加”)
或者您也可以为单选按钮提供一个 id 作为“add”,然后使用相同的代码;
即,按如下方式添加 id 并更改 name 属性的值。
struts radio 标签中的
id="add" name="addValue"
。我认为这会起作用。
You have given radio button name as "add" but while accessing in javascript you are trying to access it by id, i.e
Either change
document.getElementById("add")
todocument.getElementByName("add")
or you can also give radio button an id as "add" and then use your same code;
i.e., add id as follows and change value of name attribute.
id="add" name="addValue"
in struts radio tag.I think this will work.
您已为 radio 元素指定了名称“add”,您需要将其替换为 id="add" 或两者兼而有之 - 因为您试图通过 id 获取元素,但它当前没有 id。
编辑 - - - -
看到渲染的 HTML 似乎通过名称获取元素值更适合 getElementsByName("add")
you have given the name "add" to the radio element, you need to either replace it with id="add" or have both - as you are trying to get element by id and it currently has no id.
edit--------
Seeing the rendered HTML it seems that getting the element value by name would be better suited getElementsByName("add")
创建函数
getSelectedRadioValue
并使用无线电组名称调用该函数。Create the function
getSelectedRadioValue
and call the function with the radio group name.