在这里,我尝试单击js函数DoSubmit()之后也单击“ submitAction”按钮,还调用了servlet的doPost()方法,但是在doPost()方法内部,request.getParameter(“ submitAction”)返回null我期望它应该根据单击的按钮返回按钮值“ Verify OTP”或“ Resend OTP”。
以下是JSP代码:
<html>
<body>
<script>
function DoSubmit()
{
var btn = document.getElementById("submitAction").value;
alert("in side the DoSubmit method and OTP is : "+myOTP+ " btn clicked: "+btn);
$.ajax({
type: "POST",
url : 'myservlet',
data : {
myOTP : myOTP
},
success : function(myOTP) {
$('#ajaxGetUserServletResponse').text(myOTP);
}
});
return true;
}
</script>
<form name="submitForm" action="/myservlet">
<div id="dialog" title="Enter the OTP">
<p id="message">Enter the One Time Password</p>
<p id="note"> OTP code generated will be valid for 10 Mins<p>
<input type="text" style="margin-left:10px" valign="right" maxlength=4 size=4 id="myotp" title="OTP" name="myOTP">
<input type="submit" name="submitAction" value="Verify OTP" onclick="DoSubmit()"/>
<input type="submit" name="submitAction" value="Resend OTP"onclick="DoSubmit()"/>
<button id="back">Back</button>
<div id="ajaxGetUserServletResponse" style="display: none;"></div>
</div>
</form>
</body>
</html>
Servlet类:
@WebServlet("/myservlet")
public class GetUserServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String submitAction = request.getParameter("submitAction");
System.out.println(" "+submitAction);
}
}
答案 0 :(得分:0)
这是错误的:
<View style={{flex:1,flexDirection:"row",justifyContent:"space-between"}}>
<View style={{flex:0.5}}>
<Text>Cancel 1</Text>
</View>
<View style={{flex:0.5,alignItems:"flex-end"}}>
<Text>Cancel 2</Text>
</View>
</View>
“ submitAction”类的按钮没有id属性... 如果不通过事件,则不能像这样传递按钮的值。 您可以通过以下方式传递它:
在输入按钮的onclick功能中添加“ this”:
var btn = document.getElementById("submitAction").value;
然后在您的函数中,您将获得如下所示的值:
<input type="submit" name="submitAction" value="Verify OTP" onclick="DoSubmit(this)"/>
(确保在函数的括号中添加“ e”。)
现在,示例的第二个问题是您正在使用表单,但同时也提出了ajax请求。您想确切地做什么?
如果要发出Ajax请求,请除去表单标签。因为每次您单击按钮时都提交表单,并且您不希望这种情况发生,因为它会更改您所在的页面。
您的html应该如下所示:
function DoSubmit(e)
{
var btn = e.value;