javascript - 如何调用 jsp 方法 onclick/on 表单提交

标签 javascript html jsp

我有一个带有这段代码的 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/

相关文章:

html - 纯 CSS 选项卡面板不起作用

javascript - 根据Internet Explorer版本使用JSP加载不同的JS

java - JQuery 更新 <c :forEach> in Spring MVC

javascript - 从多个复杂子组件获取值的最佳方式?

javascript - jQuery 和 mysql 在点击时更新数据

html - 使用 CSS Hover 指定特定位置的静态图像位置

java - 在 Jboss EAP 中遇到 IllegalArgumentException

javascript - 为什么元素的属性出现在内联函数的范围内?

javascript - 如何使用ajax上传图片到MySql DB......?

c# - 如何将服务器端属性值传递到 aspx 页面框架中加载的其他 htm 页面