日期:2014-05-20  浏览次数:20853 次

struts 2 radio 的值,传到JS里面出错
我想将一个JSP页面中的radio的值传到JS中,并将选中的值作为一个参数,传到一个链接中,但是,不知道为什么,一直报错。跪求各路大神帮忙。下面是代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<%@ taglib uri="/struts-tags" prefix="struts"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
   
  <title>登陆界面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">


<script type="text/javascript">
function forgetpassword()
{
var a = document.getElementById("username").value;
if(a=="" || a==null)
{
alert("用户名不能为空");
}
else
{
var b = document.getElementById("type");
for(i=0;i<b.length;i++)
{
if(b[i].checked)
{
break
}
};
if(i==b.length)
{
alert("用户类型不能为空");
}
else
{
window.location.href="ForgetPassword!execute.action?username="+document.getElementById("username").value + "&type=" + b[i].value;
}
}
}

  </script>


  </head>
  
  <body>
  
  <struts:form action="Login">
  <struts:label value="登陆系统"></struts:label>
  <struts:textfield name="username" label="账号" id="username"/>
  <struts:password name="password" label="密码" />
<struts:radio list="#{'student':'学生','teacher':'教师','manager':'管理员'}" name="type" id="type" ></struts:radio>
<struts:submit type="button" value="登陆"></struts:submit>
<struts:reset type="button" value="重置"></struts:reset>

</struts:form>
<a href="javascript:forgetpassword()" >忘记密码</a>
  
  
  
  
  </body>
</html>
这里的问题,就是Javascript中的function forgetpassword() 里面,但是,我不知道为什么错,跪求各路大神帮忙。

------解决方案--------------------
var b = document.getElementById("type");
改成
var b = document.getElementsByName("type");
------解决方案--------------------
貌似getElementById 和getElementByName 返回的都是名为type的第一个radiobutton的值。
getElementsByName才能返回所有的radiobutton