Javascript 提交按钮赢得';t触发器ajax函数

Javascript 提交按钮赢得';t触发器ajax函数,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我的代码有一些问题,我试图将我的表单函数分离到另一个php文件,并使用ajax函数根据ID调用表单。但是,在我分离表单函数之后,submit按钮无法调用ajax函数 下面是我用来触发ajax函数并将表单从另一个php显示到shandor.php的shandor.php示例代码: <!-- Ajax show form function--> <script> function showSponsor(str) { if (str == "") {

我的代码有一些问题,我试图将我的表单函数分离到另一个php文件,并使用ajax函数根据ID调用表单。但是,在我分离表单函数之后,submit按钮无法调用ajax函数

下面是我用来触发ajax函数并将表单从另一个php显示到shandor.php的shandor.php示例代码:

<!-- Ajax show form function-->
<script>
function showSponsor(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getSponsorForm.php?sponsor="+str,true);
        xmlhttp.send();
    }
}
</script>

我做错了什么?请引导我完成。谢谢:)

尝试将表单数据序列化 然后

$.get('/url', serializedData, callback);
即:


脚本不应该返回带有
的文档,因为您正在将其放入
元素的
内部HTML
。您的意思是我应该从getSponsorForm.php中删除body和head标记吗?我应该用哪行代码替换?谢谢
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<?php
$q = intval($_GET['sponsor']);

include 'dbConnection.php';

global $dbLink;


$query="SELECT * FROM sponsor_item WHERE sponsor_item_id = '".$q."'";
$result = $dbLink->query($query);



// Numeric array
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){

echo '

<!--Banner Item No 1 Start-->
                            <div class="box box-primary1">
                                <div class="box-header">
                                    <h3 class="box-title">Edit Sponsor No.'.$row["sponsor_item_id"].' <small>编辑器</small></h3>
                                </div>
                                <div class="box-body">
                                <form class="form" id="form" action="" method="POST" enctype="multipart/form-data">
                                    <div class="box-body">
                                        <input type="hidden" name="sponsor_id" value="'.$row["sponsor_item_id"].'"></input>
                                        <div class="form-group" >
                                            <label for="sponsorTitle">Sponsor Title 赞助称号</label>
                                            <input type="text" class="form-control" name="sponsorTitle" id="sponsorTitle" placeholder="Please Enter Name" onChange="checkDisabled(testing);">
                                        </div>
                                        <div class="form-group" >
                                            <label for="sponsorDescription">Sponsor Description 赞助描述</label>
                                            <input type="text" class="form-control" name="sponsorDescription" id="sponsorDescription" placeholder="Please Enter Name" onChange="checkDisabled(testing);">
                                        </div>
                                         <div class="form-group">
                                            <label for="exampleInputFile">File input</label>
                                            <input type="file" id="uploaded_file" name="uploaded_file"  onChange="checkDisabled(testing);"><br>
                                            <p class="help-block">Your picture size not more than 2MB.  (Only JPEG or JPG is allowed)</p>
                                        </div>  

                                          <div class="checkbox">
                                            <button id="testing" type="submit" class="btn btn-primary">Update</button>       
                                        </div>
                                    </div><!-- /.box-body -->


                                </form>                  <!-- Date range -->
                                    <!-- /.form group -->

                                    <!-- Date and time range -->




                                    <!-- /.form group -->

                                    <!-- Date and time range -->
                                    <!-- /.form group -->

                                </div><!-- /.box-body -->
                            </div><!-- /.box -->
                            <!--Banner Item No 1 End-->';

}       
mysqli_free_result($result);                            
// Close the mysql connection
$dbLink->close();
?>
</body>
</html>
//File and text upload with formDATA function
                $("form#form").submit(function(){
                var formData = new FormData($(this)[0]);    
                    $.ajax({
                        url:'sponsorItem.php',
                        type: 'POST',
                        data: formData,
                        async: false,
                        beforeSend: function(){
                         if(confirm('Are you sure?'))
                              return true;
                          else
                              return false;
                        },
                        cache: false,
                        contentType: false,
                        processData: false
                    }).done(function () {
                            //do something if you want when the post is successfully
                            if(!alert('Banner Had Successfully Updated.')){document.getelementbyclassname('form').reset()}
                        });
                    return false;

            });
$.get('/url', serializedData, callback);
$( "form#form" ).on( "submit", function( event ) {
  event.preventDefault();
  var data = $(this).serialize();
  $.post('sponsorItem.php', data, function(response){
     $('#txtHint').text(response);
  });
});