[英]Struts 2 get radio button value using javascript
I have created a radio button using Struts tags however i am attempting to get the selected value of the radio button however i am unable to get the value. 我已经使用Struts标签创建了一个单选按钮,但是我试图获取单选按钮的选定值但是我无法获得该值。 Under is what i have done thus far.
Under是我到目前为止所做的。
Jsp JSP
<s:radio name="gender" id= "gender" list="#{'1':'Male','2':'Female'}" onChange="genderChange()"/>
Javascript function Javascript功能
function genderChange(){
var gendervalue;
genderValue = document.getElementById("gender").value;
alert(gendervalue);
}
Console Error 控制台错误
TypeError: document.getElementById(...) is null
The solution was for me to pass the element value into the java script function under is my updated code 解决方案是让我将元素值传递给我更新的代码下的java脚本函数
Jsp JSP
<s:radio name="gender" id= "gender" list="#{'1':'Male','2':'Female'}" onChange="genderChange(this.value)"/>
Javascript function Javascript功能
function genderChange(value){
var gendervalue;
genderValue = value;
alert(gendervalue);
}
If you see your page in HTML you can see that two radio buttons will be create, with 2 different ids: gender1 and gender2. 如果您在HTML中看到您的页面,您会看到将创建两个单选按钮,其中包含两个不同的ID:gender1和gender2。 You can access to your value with:
您可以通过以下方式访问您的价值:
var foo = document.getElementById("gender1").checked;
In this case, if the first is checked (foo==true), the other will be not checked. 在这种情况下,如果检查第一个(foo == true),则不检查另一个。
A more scalable solution is using: 更具可扩展性的解决方案是:
document.getElementsByName("gender")[index].checked
where index is a number that indentify all radio buttons with the name "gender". 其中index是一个数字,用于标识名称为“gender”的所有单选按钮。 You can make a FOR to cycle the array and find at which index checked is true.
您可以使FOR循环数组并找到检查的索引是否为true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.