最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to invoke a jsp method onclickon form submit - Stack Overflow

programmeradmin6浏览0评论

I have a jsp page with this code:

<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.

how can i call Withdraw() after button click?

10x

I have a jsp page with this code:

<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.

how can i call Withdraw() after button click?

10x

Share Improve this question edited May 3, 2012 at 19:09 WordToTheWise asked May 3, 2012 at 19:00 WordToTheWiseWordToTheWise 932 gold badges2 silver badges7 bronze badges 5
  • simply adding onClick="Withdraw();" in the input element should be enough. – Denys Séguret Commented May 3, 2012 at 19:02
  • 2 @dystroy — It is server side code, so no, that shouldn't work. – Quentin Commented May 3, 2012 at 19:13
  • doesn't work. starting to wonder if it's even possible.. – WordToTheWise Commented May 3, 2012 at 19:21
  • I don't understand the problem. Do you want to call the browser side function of the server side's one ? My ment was for calling the browser side's one. – Denys Séguret Commented May 3, 2012 at 19:23
  • server side function.. the problem is that i can't call the method Withdraw() on form submit. – WordToTheWise Commented May 3, 2012 at 19:30
Add a ment  | 

1 Answer 1

Reset to default 3

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.

Second you are setting a type. Don't you want to set the value?

So the HTML would look like

<input type="hidden" name="hidden" id="hidden" value="" />

and the JavaScript would be

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.

发布评论

评论列表(0)

  1. 暂无评论