admin管理员组文章数量:1417040
I try to post a form using ajax the current code does not really work. When I press the save button the form get submitted n + 1 times. i.e After refreshing the page it submit once, next time I submit two form get submitted, third time... etc.
I have spend a lot of time researching this already (2 days) and I have not found a questions quite similar to what I am asking.
I am on a steep learning curve here so I hope someone can point out to me what I am doing wrong.
I think I might have mixed something up. The steps up to submit is.
- Form values is being filled in.
- A button is pressed to show a modal to confirm to submit the form (The submit button is actually inside this modal and not inside the form itself).
- Form is submitted.
$('#confirmYes').click(function() {
$('#confirm-object').modal('hide'); // close confirm modal
$('#newForm').submit(function (e) {
e.preventDefault();
let formData = $(this).serialize();
$.post({
type: 'POST',
url: '/api/pois/',
data: formData
})
<form id="newForm">
<input type="text" id="name" name="name">
<input type="text" id="pany" name="pany">
</form>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-success" form="newForm" id="confirmYes">Save</button>
</div>
I try to post a form using ajax the current code does not really work. When I press the save button the form get submitted n + 1 times. i.e After refreshing the page it submit once, next time I submit two form get submitted, third time... etc.
I have spend a lot of time researching this already (2 days) and I have not found a questions quite similar to what I am asking.
I am on a steep learning curve here so I hope someone can point out to me what I am doing wrong.
I think I might have mixed something up. The steps up to submit is.
- Form values is being filled in.
- A button is pressed to show a modal to confirm to submit the form (The submit button is actually inside this modal and not inside the form itself).
- Form is submitted.
$('#confirmYes').click(function() {
$('#confirm-object').modal('hide'); // close confirm modal
$('#newForm').submit(function (e) {
e.preventDefault();
let formData = $(this).serialize();
$.post({
type: 'POST',
url: '/api/pois/',
data: formData
})
<form id="newForm">
<input type="text" id="name" name="name">
<input type="text" id="pany" name="pany">
</form>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-success" form="newForm" id="confirmYes">Save</button>
</div>
Share
Improve this question
edited Feb 14, 2018 at 8:31
Vadim Kotov
8,2848 gold badges50 silver badges63 bronze badges
asked Feb 14, 2018 at 8:30
geogrowgeogrow
5152 gold badges11 silver badges28 bronze badges
3 Answers
Reset to default 3The issue is because you are creating a new submit
event handler in every click. From the description of what you want to do, you instead need to create a single submit handler when the page loads, and trigger it when the button is clicked. Something like this:
$('#newForm').submit(function(e) { // handle the submit event
e.preventDefault();
let formData = $(this).serialize();
$.post({
type: 'POST',
url: '/api/pois/',
data: formData
})
})
$('#confirmYes').click(function() {
$('#confirm-object').modal('hide');
$('#newForm').submit(); // trigger the submit event
});
Simply remove the $('#newForm').submit(function (e) {});
:
.submit(function (e) {})
is creating an event handler for the submit
event of your form, it's not submitting it.
$('#confirmYes').click(function() {
$('#confirm-object').modal('hide'); // close confirm modal
let formData = $('#newForm').serialize();
$.post({
type: 'POST',
url: '/api/pois/',
data: formData
});
});
$('#confirmYes').click(function() {
let formData = $('#newForm').serialize();
$.post({
type: 'POST',
url: '/api/pois/',
data: formData
});
);
<form id="newForm">
<input type="text" id="name" name="name">
<input type="text" id="pany" name="pany">
</form>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-success" id="confirmYes">Save</button>
</div>
本文标签: javascriptsubmit form with button outside form using ajaxStack Overflow
版权声明:本文标题:javascript - submit form with button outside form using ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745263156a2650450.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论