javascript - jquery click() in submit button -
i have .click() function on submit button in form:
$("#submitid").click(function () { $('#hiddeninput').val(somevariable); });
it works charm. when submit button clicked click() function fills hidden input variable , whole form gets submitted. server receives hidden input refreshed content.
my question is: work? there danger that, reason not yet known me, submit operation gets executed first , click() function later? want create sure hidden input gets refreshed.
whenever working forms, it's always advisable allow submit
button it's job: trigger form's submit event ... that's it's meant for. hear submit
event on form
rather click
event on submit
button.
you can utilize event.preventdefault()
prevent submission of form can house keeping can submit form.
$("#submitid").closest('form').on('submit', function(e) { e.preventdefault(); $('#hiddeninput').val(somevariable); //perform operations this.submit(); //now submit form });
or simply,
$('form').on('submit', function(e) { e.preventdefault(); $('#hiddeninput').val(somevariable); //perform operations this.submit(); //now submit form });
javascript jquery forms click submit
No comments:
Post a Comment