我有一个带有这段代码的 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);
} %>
我需要在提交表单时激活 Withdraw() 方法并获取文本输入。 javascript 保存插入“隐藏”的值,我可以稍后访问它。 但我不能调用:<% Withdraw(); %> 来自 javascript 内部。
如何在点击按钮后调用 Withdraw()?
10倍
最佳答案
首先你的代码行有问题
document.getElementById('hidden').type = withdraw;
它正在寻找一个 id 为 hidden 的元素。不是名字,是ID。因此,向您引用的元素添加一个 id。
其次,您要设置一个类型。您不想设置值吗?
所以 HTML 看起来像
<input type="hidden" name="hidden" id="hidden" value="" />
JavaScript 将是
document.getElementById('hidden').value = withdraw;
现在如果你想在服务器上调用一个函数,你需要回发表单或进行 Ajax 调用。
关于javascript - 如何调用 jsp 方法 onclick/on 表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10437717/