日期:2014-05-17  浏览次数:20743 次

◆谁能告诉我这是怎么回事,菜鸟提问◆
我想做这样的测试:当分别点击按钮1和按钮2时,提交不同的数据给服务端处理后,服务端根据不同的值返回js提示语句,
即:
点击按钮1服务端执行out.print("<script>alert('按钮1验证!');</script>");
点击按钮2服务端执行out.print("<script>alert('按钮2验证!');</script>");

jsp代码如下,不管我点击按钮1还是按钮2,总是执行out.print("<script>alert('"+b+"');</script>");,,但显示的b值又是传过去的"b1"或者"b2",不知为何?
Java code
<%@ page contentType="text/html;charset=GB2312"%>
<html>
<head>
 <title>当前页面验证</title>
</head>
 <script type="text/javascript" >
 // document.getElementsByName("ba")[0].value = "";
  function test1()
  {
   document.getElementsByName("yanzh")[0].value="b1";
   bd1.submit();
  }
  
  function test2()
  {
   document.getElementsByName("yanzh")[0].value="b2";
    bd1.submit();
  }
</script>

<body>
<form  name="bd1"  method="post" >
<input type="text" name = "yanzh" style="display:none"/>
<input type="button" value="按钮1验证" onclick = " test1()"/>
<input type="button" value="按钮2验证" onclick = " test2()"/>
<input type="text" name="text1" id = "wb1" />
<%
  String b = request.getParameter("yanzh");
  if (b=="b1") 
       out.print("<script>alert('按钮1验证!');</script>");
  else if (b=="b2") 
       out.print("<script>alert('按钮2验证!');</script>");
  else
       out.print("<script>alert('显示b值:"+b+"');</script>");
%>
</form>
</body>
</html>


------解决方案--------------------
改成这样
Java code

  if ("b1".equals(b)) 
       out.print("<script>alert('按钮1验证!');</script>");
  else if ("b2".equals(b)) 
       out.print("<script>alert('按钮2验证!');</script>");
  else
       out.print("<script>alert('显示b值:"+b+"');</script>");

------解决方案--------------------
探讨

改成这样
Java code

if ("b1".equals(b))
out.print("<script>alert('按钮1验证!');</script>");
else if ("b2".equals(b))
out.print("<script>alert('按钮2验证!');</script>");
else
out.pr……