2013-12-16 38 views
1

jsp中有两个按钮如何知道单击的是哪个按钮,JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
    <body style="background-color:black;"> 


<p> 
<p><input type="button" name="good" value="Pen" onclick="location.href='hello';"> </p> 
<p><input type="button" name="good" value="Paper" onclick="location.href='hello';"> 
</p> 

</body> 
</html> 

这是servlet

package pack.exp; 
import java.io.IOException; 
import javax.servlet.http.*; 

import com.google.appengine.api.datastore.DatastoreService; 
import com.google.appengine.api.datastore.DatastoreServiceFactory; 
import com.google.appengine.api.datastore.Entity; 
import com.google.appengine.api.datastore.EntityNotFoundException; 
import com.google.appengine.api.datastore.Key; 
import com.google.appengine.api.datastore.KeyFactory; 


@SuppressWarnings("serial") 
public class HelloServlet extends HttpServlet 
{ 

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws 
    IOException 
{ 
    String val=req.getParameter("good"); 
    if("Pen".equals(val)) 
    { 
     resp.setContentType("text/plain"); 
     resp.getWriter().println("Pen was clicked"); 
    } 
    else if("Paper".equals(val)) 
    { 
     resp.setContentType("text/plain"); 
     resp.getWriter().println("Paper was clicked"); 
    } 

} 
} 

我的代码是不是给上点击按钮正确的输出。我想当我点击笔,然后它应该输入if()和打印文本,我想为纸张按钮相同。

回答

0

添加两个隐藏字段。

<form id="my form" > 
<input type="hidden" name="myPen" /> 
<input type="hidden" name="myPaper" /> 
<input type="button" name="good" value="pen" on click="{document.myform.mypen.value=this.value;location.href='hello';}" 
<input type="button" name="good" value="paper" on click=" {document. myform. mypaper.value=this.value;location. href='hello';}" /> 
</form> 

在Servlet的

String val=req.getParameter('mypen'); 
    String val1=req.getParameter("mypaper"); 

在时间有其它只有一个值是null。你可以用null check来做到这一点。

1

使用jQuery来完成您的任务。

将您的html代码改为这些代码行。

<form method="post" action="#" name="Form" id="Form" > 
<input type="button" value="one" id="One"/> 
<input type="button" value="two" id="Two"/> 
</form> 

,并添加脚本这些行

$('input:button').click(function() { 
alert($(this).val()); 
var value=$(this).val(); 
var url='hello?good='; 
url+=value; 
$("#Form").attr("action",url); 
$("#Form").submit(); 
}); 

您可以使用jQuery 1.7.1及以上。 希望这可以帮助你。快乐编码:)

+0

嘿,我没有JQuery的知识。你能解释一下吗? – Sandeep

+0

如果您只想使用html,则需要使用单选按钮。我不确定是否有其他方法。 jquery很容易使用。你正确的复制粘贴我的jQuery代码到HTML的脚本标签。 供参考,你可以参考一个示例jquery示例。 [jquery示例Click Me](http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide)请点击此链接。 – Bharathi

0

将按钮的值编码为按钮的名称。

在HTML:

<p><input type="button" name="good:Pen" onclick="location.href='hello';"> </p> 
<p><input type="button" name="good:Paper" onclick="location.href='hello';"> 

在servlet的:遍历所有参数,寻找参数,startsWith("good"),如果是,副轨道前缀good:。如果页面中没有其他提交按钮,则只需简单地命名按钮PenPaper,并检查是否存在这些参数。这项任务没有需要的JavaScript。

0

写这段代码在JSP中,这将有助于你

<html> 
<body style="background-color:black;"> 

<p><form method="post" action="hello"> 
<p><input type="submit" name="good" value="Pen" > </p> 
<p><input type="submit" name="good" value="Paper" > 
</p></form> 

</body> 
</html> 
相关问题