我想知道如何在jsp中设置不同的按钮,以及如何为我们按下的按钮提交相应的页面。
我试过停止代码:
<html>
<head> </head>
<body>
<%-- scriptlet here --%>
<%
if(request.getParameter("btnSubmit_1")!=null)
{
// means submit_1 is pressed.
// in java script we do give action as fallows
/**document.frm2.action="sub-faconline.jsp";
//document.frm2.target="right_f";
//document.frm2.submit();****
my question how can we do above java script code in scriplet in jsp
}
else if(request.getParameter("btnSubmit_2")!=null)
{
// i want to give some jsp page name here for action.
}
%>
<form name="frm2" method="post">
<input type="button" id="btnSubmit_1" name="btnSubmit_1" value="btnSubmit_1" />
<input type="button" id="btnSubmit_2" name="btnSubmit_2" value="btnSubmit_2"/>
</form>
</body>
</html>
答案 0 :(得分:0)
您必须将html修改为:
<form name="frm2" method="post" action="myJsp.jsp">
<input type="button" id="btnSubmit_1" name="btnSubmit_1" onClick="submitForm('btnSubmit_1'); value="btnSubmit_1"" />
<input type="button" id="btnSubmit_2" name="btnSubmit_2" onClick="submitForm('btnSubmit_2'); value="btnSubmit_2"/>
<input type="hidden" id="btnPressed" name="btnPressed" value=""/>
</form>
使用Javascript:
function submitForm(elem){
document.getElementById("btnPressed").value=elem;
document.frm2.submit();
}
然后在你的jsp中你可以得到按下的按钮值:
request.getParameter("btnPressed");
说明:在将保存btn值的表单中创建一个不可见的输入。然后将onclick功能附加到每个将设置隐藏输入值的按钮。然后将表单提交到表单的操作URL。然后在该URL中,您可以获取隐藏输入的值(包含按下的按钮的值)