javascript 如何调用jsp方法onclick/on form submit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10437717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to invoke a jsp method onclick/on form submit
提问by WordToTheWise
I have a jsp page with this code:
我有一个带有以下代码的jsp页面:
<script type="text/javascript">
function getWithdrawAmmount()
{
var withdraw=document.forms["WithdrawDeposit"]["AmountToWithdraw"].value;
document.getElementById('hidden').type = withdraw;
}
</script>
<form method="POST" name="WithdrawDeposit" onsubmit="getWithdrawAmmount()">
<table>
<tr><td><input type="text" size=5 name="AmountToWithdraw"></td>
<td><input type="button" value="Withdraw"></td></tr>
</table>
</form>
<input type="hidden" name="hidden" value="">
<% String AmountWithdraw = request.getParameter("hidden"); %>
<%!
public void Withdraw(){
int Amount = Integer.parseInt("AmountWithdraw");
Deposit deposit = new Deposit();
deposit.WithdrawMoney(AmountWithdraw);
} %>
I need to activate the Withdraw() method on form submit and get the text input. the javascript hold the value inserted in 'hidden' and i can access it later. but i can't call to : <% Withdraw(); %> from inside javascript.
我需要在表单提交时激活 Withdraw() 方法并获取文本输入。javascript 保存插入到“隐藏”中的值,我可以稍后访问它。但我不能打电话给:<% Withdraw(); %> 来自 javascript 内部。
how can i call Withdraw() after button click?
单击按钮后如何调用 Withdraw()?
10x
10倍
回答by epascarello
First off your line of code has issues
首先你的代码行有问题
document.getElementById('hidden').type = withdraw;
It is looking for an element with an id of hidden. Not a name, an id. So add an id to the element you are referencing.
它正在寻找一个 id 为 hidden 的元素。不是名字,是身。因此,为您正在引用的元素添加一个 id。
Second you are setting a type. Don't you want to set the value?
其次,您正在设置类型。你不想设置值吗?
So the HTML would look like
所以 HTML 看起来像
<input type="hidden" name="hidden" id="hidden" value="" />
and the JavaScript would be
JavaScript 将是
document.getElementById('hidden').value = withdraw;
Now if you want to call a function on the server, you either need to post back the form or make an Ajax call.
现在,如果要调用服务器上的函数,则需要回发表单或进行 Ajax 调用。