jquery ajax form submit

前端 未结 3 1035
说谎
说谎 2020-12-04 00:22

Is this code correct? I\'m trying to submit it and also I would like on submit if text area would be empty again.



        
相关标签:
3条回答
  • 2020-12-04 01:01
    ​$(function(){
        $("form#submit").submit(function(e){
            e.preventDefault();
            var fid = $(".messag").attr("id");
            var val = $("#mess_ar").val();
            $.ajax({
                type: "POST",
                url: "send_message.php",
                data: "fid="+ fid +"&val="+ val,
                success: function(){
                    $("#mess_ar").val("");
                }
            });
        });
    });​
    

    Use

    $("#submit").on('submit', function(e){...});
    

    instead of

    $("#submit").submit(function(e){...});
    

    if you are using latest version of jquery.

    0 讨论(0)
  • 2020-12-04 01:01

    What are you actually looking for? Does it return the data in response too? Add the functions to track your the error case too. Make something like

    <script type="text/javascript">
    $(document).ready(function(){
    
        $("form#submit").submit(function() {
           // we want to store the values from the form input box, then send via ajax below
           var fid = $(".messag").attr("id");
           var val = $("#mess_ar").val();
           $.ajax({
              type: "POST",
              url: "send_message.php",
              data: "fid="+ fid +"&val="+ val,
              success: function(incoming_data){
                 // ALERT incoming data if coming
                 $("#mess_ar").text(""); // DO YOUR JOB CONTINUOU
              },
              error: function() { 
                 alert("BROKEN REQUEST.");
              }
           });
           return false;
       });
    
    });
    </script>
    

    Else, seems all fine.

    0 讨论(0)
  • 2020-12-04 01:04

    Looks good. To empty the textarea use the following in the ajax success callback:

    $("#mess_ar").val('');
    
    0 讨论(0)
提交回复
热议问题