簡體   English   中英

jQuery Ajax PHP只工作一次

[英]Jquery ajax php only working once

我有以下jQuery

        $("button#submit").click(function(){
            $.ajax({
                type: "POST",
                url: "process.php",
                data: $('form.contact').serialize(),
              success: function(msg){
                $("#form-content").modal('hide');                     
                $("#thanks").html(msg);
                $("#thanks").delay(2000).fadeOut("slow");
              },
                error: function(){
                    alert("failure");
                }
            });
        });

和PHP

<?php
    if (isset($_POST['name'])) {
    $name = strip_tags($_POST['name']);
    $email = strip_tags($_POST['Email']);
    $sug = strip_tags($_POST['sug']);

    echo "<span class='label label-info'>Your Website has been submitted .. Thank you</span>";
}?>

這是第一次工作,並在我的頁面上顯示php echo。 但是,當我再次提交表單時,它不會顯示。

您的$("#thanks") dom隱藏。

    $("button#submit").click(function(){
        $.ajax({
            type: "POST",
            url: "process.php",
            data: $('form.contact').serialize(),
          success: function(msg){
            $("#form-content").modal('hide');                     
            $("#thanks").html(msg);
            $("#thanks").show();  <----------------ADD THIS
            $("#thanks").delay(2000).fadeOut("slow");
          },
            error: function(){
                alert("failure");
            }
        });
    });

使用.on()

$(document).on('click','button#submit',function(){  })

這樣使用

$("button#submit").live('click', function(e) {
  $.ajax({
            type: "POST",
            url: "process.php",
            data: $('form.contact').serialize(),
          success: function(msg){
            $("#form-content").modal('hide');                     
            $("#thanks").html(msg);
            $("#thanks").delay(2000).fadeOut("slow");
          },
            error: function(){
                alert("failure");
            }
        });
    });

在這里檢查差異.live.on jQuery .live()與.on()方法,以在加載動態html后添加click事件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM