使用 javascript 访问单选按钮状态

发布于 2024-11-30 22:27:36 字数 609 浏览 5 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她如夕阳 2024-12-07 22:27:36

您已将单选按钮名称指定为“add”,但在 javascript 中访问时,您尝试通过 id 访问它,即将

document.getElementById("add");

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

document.getElementById("add");

Either change document.getElementById("add") to document.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.

弥繁 2024-12-07 22:27:36

您已为 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")

手心的海 2024-12-07 22:27:36

创建函数 getSelectedRadioValue 并使用无线电组名称调用该函数。

function getAddress()
{
    var add=getSelectedRadioGroupValue("add");
    alert(add);
}

//Method to return radio group value. default 0
function getSelectedRadioGroupValue(name){

    var radioGrArr = document.getElementsByName(name);
    var value = '0';
    for (var i=0; i < radioGrArr .length; i++) {
    if (radioGrArr [i].checked == true) {
            value = radioGrArr [i].value;
            break;
        }
    }
    return value;
}

Create the function getSelectedRadioValue and call the function with the radio group name.

function getAddress()
{
    var add=getSelectedRadioGroupValue("add");
    alert(add);
}

//Method to return radio group value. default 0
function getSelectedRadioGroupValue(name){

    var radioGrArr = document.getElementsByName(name);
    var value = '0';
    for (var i=0; i < radioGrArr .length; i++) {
    if (radioGrArr [i].checked == true) {
            value = radioGrArr [i].value;
            break;
        }
    }
    return value;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文