Ajax提交所有表格 [英] Ajax submit for all forms
问题描述
我有一个servlet,它对db执行一些操作,并将其重定向到一个jsp.现在,在这个jsp中,我有几种形式可以进行不同的操作. 我制作了一个脚本,无需刷新即可将数据插入db.问题是该脚本正在为所有表单而不只是一个表单(我想要的表单)插入数据. 这是我的jsp代码:
I have a servlet that executes some operations on db, and the it is redirected to a jsp. Now in this jsp I have several forms that make different operations. I made a script that inserts data into db without refreshing.The problem is that this script is inserting data for all forms not for just one(that form that I want). Here is my jsp code:
<form action="propunereteta" method="post" id="bookingForm">
<div class = "tip">
<strong> <select name="tip11">
<option>Tipul preparatului!</option>
<option>Mic dejun</option>
<option>Pranz</option>
<option>Cina</option>
</select></strong>
</div>
<a><button type = "submit" class="btnret" >Propune</button></a>
</form>
<form action="nota" method="post">
<div class="nota"><br /><br />
<div class="grid_6">
<input type="hidden" name="reteta1" value="${Nume} "/>
<input class="star star-5" id="star-5" type="radio" name="mark" value="100"/>
<label class="star star-5" for="star-5"></label>
<input class="star star-4" id="star-4" type="radio" name="mark" value="75"/>
<label class="star star-4" for="star-4"></label>
<input class="star star-3" id="star-3" type="radio" name="mark" value="50"/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" id="star-2" type="radio" name="mark" value="25"/>
<label class="star star-2" for="star-2"></label>
<input class="star star-1" id="star-1" type="radio" name="mark" value="0"/>
<label class="star star-1" for="star-1"></label>
</div>
</div>
<a><button type = "submit" class="btnret" >Noteaza</button></a>
</form>
这是脚本:
$("form").submit(function (e) {
e.preventDefault();
$.post($(this).attr("action"), $(this).serialize(), function () {
alert("Submitted!");
});
});
现在我希望只对最后一种形式执行该脚本,现在对所有形式都执行.
Now I want that script to be executed only for the last form, now is executed for all forms.
有人可以帮助我吗?谢谢
Could anyone help me? Thanks
推荐答案
为两个表单添加id
并为每个表单编写ajax函数.
Add id
for both the forms and write ajax function for each of the forms.
这是第一个表格
<form action="propunereteta" method="post" id="firstForm">
//form code
</form>
第二个
<form action="nota" method="post" id="secondForm">
//form code
</form>
然后是两个脚本
$("#firstForm").submit(function (e) {
//do something
});
$("#secondForm").submit(function (e) {
//do something
});
这篇关于Ajax提交所有表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!