admin管理员组文章数量:1303448
I have the following AJAX script that I am using to return a random PIN number to a user when they register their email address:
HTML
<form class="form-inline" role="form" id="formRegister">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="button" name="srSubmit" onClick="getSubmitResponse();" class="btn btn-default btn-lg">Generate PIN</button>
</form>
JS
function getSubmitResponse(){
var emailId = $("#srEmail").val();
$.ajax({
type: "POST",
url: "register-ajax-call.php",
data: { srEmail: emailId, submiType: 'srSubmit'}
})
.done(function( html ) {
if(html.search('PIN:')!=-1){
window.location = "success.php?response="+html;
}else{
$( "#results").empty();
$("#results").append( html );
}
});
}
Question: how can I use button type="submit" rather than type="button" in this situation? My reasoning is that this is better markup for form validation and more consistent behaviours (e.g. when a user hits ENTER).
I am also using jQuery if that is a solution.
I have the following AJAX script that I am using to return a random PIN number to a user when they register their email address:
HTML
<form class="form-inline" role="form" id="formRegister">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="button" name="srSubmit" onClick="getSubmitResponse();" class="btn btn-default btn-lg">Generate PIN</button>
</form>
JS
function getSubmitResponse(){
var emailId = $("#srEmail").val();
$.ajax({
type: "POST",
url: "register-ajax-call.php",
data: { srEmail: emailId, submiType: 'srSubmit'}
})
.done(function( html ) {
if(html.search('PIN:')!=-1){
window.location = "success.php?response="+html;
}else{
$( "#results").empty();
$("#results").append( html );
}
});
}
Question: how can I use button type="submit" rather than type="button" in this situation? My reasoning is that this is better markup for form validation and more consistent behaviours (e.g. when a user hits ENTER).
I am also using jQuery if that is a solution.
Share Improve this question asked Sep 21, 2013 at 20:12 alias51alias51 8,64822 gold badges106 silver badges185 bronze badges3 Answers
Reset to default 3You can use .submit()
method of jQuery like this
$("#formRegister").submit(function(event) {
var form = $( this ),
url = form.attr( 'action' );
var posting = $.post( url, { srEmail: ("#srEmail").val() } );
posting.done(function( data ) {
$("#results").append( html );
});
});
However you should think of giving the validation in the Client side, before submitting it. Since you already use jQuery, I would suggest you to take a look a jQuery.validate
incase your form has more attributes.
Just use onsubmit event and not onclick event.
http://www.w3schools./jsref/event_form_onsubmit.asp
Then in your ajax function just return false; like that the form will not be send like usually and the browser will not refresh the page.
Hope it helps
You can do like this:
jQuery:
$("#formRegister").on("submit", function() {
$.ajax({
// Rest of code
});
return false;
});
本文标签: javascriptHow to use input typesubmit with AJAX formStack Overflow
版权声明:本文标题:javascript - How to use input type=submit with AJAX form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741745929a2395530.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论