如何使用输入类型=使用AJAX表单提交

时间:2022-11-24 14:22:19

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:

我有以下AJAX脚本,用于在用户注册其电子邮件地址时向用户返回一个随机PIN码:

HTML

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

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

问题:在这种情况下,如何使用button type =“submit”而不是type =“button”?我的理由是,这是表单验证和更一致行为的更好标记(例如,当用户点击ENTER时)。

I am also using jQuery if that is a solution.

我也在使用jQuery,如果这是一个解决方案。

3 个解决方案

#1


2  

You can use .submit() method of jQuery like this

你可以像这样使用jQuery的.submit()方法

 $("#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.

但是,在提交之前,您应该考虑在客户端进行验证。既然你已经使用了jQuery,我建议你看一下jQuery.validate,你的表单有更多的属性。

#2


1  

Just use onsubmit event and not onclick event.

只需使用onsubmit事件而不是onclick事件。

http://www.w3schools.com/jsref/event_form_onsubmit.asp

http://www.w3schools.com/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.

然后在你的ajax函数中返回false;就像那样,表格不会像往常一样发送,浏览器也不会刷新页面。

Hope it helps

希望能帮助到你

#3


1  

You can do like this:

你可以这样做:

jQuery:

jQuery的:

$("#formRegister").on("submit", function() {

   $.ajax({ 

       // Rest of code 
   });

return false;
});

#1


2  

You can use .submit() method of jQuery like this

你可以像这样使用jQuery的.submit()方法

 $("#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.

但是,在提交之前,您应该考虑在客户端进行验证。既然你已经使用了jQuery,我建议你看一下jQuery.validate,你的表单有更多的属性。

#2


1  

Just use onsubmit event and not onclick event.

只需使用onsubmit事件而不是onclick事件。

http://www.w3schools.com/jsref/event_form_onsubmit.asp

http://www.w3schools.com/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.

然后在你的ajax函数中返回false;就像那样,表格不会像往常一样发送,浏览器也不会刷新页面。

Hope it helps

希望能帮助到你

#3


1  

You can do like this:

你可以这样做:

jQuery:

jQuery的:

$("#formRegister").on("submit", function() {

   $.ajax({ 

       // Rest of code 
   });

return false;
});