如何知道在JSP上单击了哪个按钮

如何知道在JSP上单击了哪个按钮

本文介绍了如何知道在JSP上单击了哪个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

带有两个按钮的Jsp

Jsp with two buttons

<%@ 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()并打印文本,并且我希望纸张按钮也是如此.

My code is not giving the correct output on clicking the buttons. I want when i click Pen then it should enter in if() and print the text and i want same for the paper button.

推荐答案

使用jquery完成任务.

Use jquery to do your task.

将您的html代码更改为这些代码行.

Change your html code to these lines of code.

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

并将这些行添加到脚本中

And add these lines in your script

$('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及更高版本.希望这对您有所帮助.快乐编码:)

You can use jquery 1.7.1 and above.Hope this helps you. Happy Coding :)

这篇关于如何知道在JSP上单击了哪个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 04:27