我想在前台提交的时候让<input name="user.id" id="id" type="text" alt="用户名">这个种会跟着radio的值改变,比如我radio如果是选的manager的话,那么<input name="user.id" id="id" type="text" alt="用户名">就会变成<input name="manager.id" id="id" type="text" alt="用户名">,于是我写了一段javascript代码,可是完全没起作用,谁能帮我看看啊 分不多,谢谢啦<%@ page pageEncoding="UTF-8"%>
<% 
String path = request.getContextPath();
%>
<html>
<head>
<title>主页</title>
<script type="text/javascript">
var id = document.getElementById("id"); 
var password = document.getElementById("password"); 
var loginType = document.getElementsByName("loginType");
alert(loginType[0].getAttribute("value"));
if(loginType[0].getAttribute("value").equles("manager"))
    {
         id.setAttribute("name", "manager.id");
         password.setAttribute("name", "manager.password");
  
    }
else
    {
id.setAttribute("name", "user.id");
    password.setAttribute("name", "user.password");
    }
</script>
</head>
<body>
<form action="login.action" method="post">
用户名:<input name="user.id" id="id" type="text" alt="用户名">
密     码:<input name="user.password" id="password" type="password" alt="密码">
<input type="radio" name="loginType" value="manager"> 管理员
<input type="radio" name="loginType" value="user"> 用户
<input type="submit" value="提交">&nbsp;&nbsp;<input type="reset" value="重置">
</form>
</body>
</html>

解决方案 »

  1.   

    <%@ page pageEncoding="UTF-8"%>
    <%  
    String path = request.getContextPath();
    %>
    <html>
    <head>
    <title>主页</title>
    <script type="text/javascript">
    function aaaa(obj) {
    var id = document.getElementById("id");  
    var password = document.getElementById("password");  
    //var loginType = document.getElementsByName("loginType");
    //alert(loginType[0].getAttribute("value"));
    alert("loginType.value:" + obj.value);
    if(obj.value == "manager")
    //if(loginType[0].getAttribute("value").equles("manager"))
      {
      id.name = "manager.id";
      password.name="manager.password";
    //  id.setAttribute("name", "manager.id");
    //  password.setAttribute("name", "manager.password");
       
      }
    else
      {
      id.name = "user.id";
      password.name="user.password";
    //id.setAttribute("name", "user.id");
    //  password.setAttribute("name", "user.password");
      }
      alert(id.name);
      alert(password.name);
    }
    </script>
    </head>
    <body>
    <form action="login.action" method="post">
    用户名:<input name="user.id" id="id" type="text" alt="用户名">
    密 码:<input name="user.password" id="password" type="password" alt="密码">
    <input type="radio" name="loginType" value="manager" onchange="aaaa(this);"> 管理员
    <input type="radio" name="loginType" value="user" onchange="aaaa(this);"> 用户
    <input type="submit" value="提交">&nbsp;&nbsp;<input type="reset" value="重置">
    </form>
    </body>
    </html>
      

  2.   

    不是很明白你要干什么
    1、你可以试试Ajax行不行
    2、使用JavaScript的话,你最好在select的OnChange方法上执行会比较好点
      

  3.   

    你在后台直接获取radio上面的值不就行啦,然后判断是manager还是user 还是推荐你用下Jquery这个很好操作的