weixin_33711641 2015-07-15 20:43 采纳率: 0%
浏览 21

不寻常的AJAX行为

Here's my HTML

<form id="the_form">
          Zip / Postal Code: <input id="zip" type="text" name="zip"><br/>
          <input type="submit" value="Submit" id="submitButton">
</form>


<div id="output"></div>

Here's my jQuery

<script type="text/javascript">
  $("document").ready(function() {

   $("#submitButton").on("click",function(){

     var value_to_send_to_php = $.trim($("#zip").val());

        if(value_to_send_to_php != "") {

              $.post("formphptoajax_rb.php",  $("#the_form").serializeArray() , function(data) {
                  //#1 doesn't work here
              });   
                  //#2 doesn't work here         
        }    
                  //#3 doesn't work here
    });

      $("#output").text("success");

  });
</script>

The above jQuery works fine. That is, on clicking submit, I get "success" output in the div with id "output".

What I can't understand is why I don't get the same result when I place $("#output").text("success"); at the locations marked #1, #2, or #3.

At #1 and #2 I get no output. At #3, I get "success" for a fraction of a second and then it vanishes.

  • 写回答

3条回答 默认 最新

  • 衫裤跑路 2015-07-15 20:49
    关注

    As well as the click handler, the page is also submitting since you have clicked a "submit" button.

    Use input type="button" instead. This is what it is designed for: a non-submitting button.

    评论

报告相同问题?