Ajax 调用不保存表单信息


Ajax call not saving form information

我尝试使用以下代码使用 ajax 保存和显示信息。但它不起作用。

这是代码。

<?php session_start();?>
<html>
<head>
<script src="style/jquery-ui.js" type="text/javascript" charset="utf-8"></script>
<script src="style/jquery-1.11.1.min.js"></script> 
<script>
$(function() {
$("#ajaxquery").live( "submit" , function(){
    // Intercept the form submission
    var formdata = $(this).serialize(); // Serialize all form data
    // Post data to your PHP processing script
    $.post( "show.php", formdata, function( data ) {
        // Act upon the data returned, setting it to #success <div>
        $("#success").html ( data );
    });
    return false; // Prevent the form from actually submitting
})
});
</script>
</head>
<form id="ajaxquery" method="post" action="">
<label for="field">Type Something:</label>
<input type="text" name="field" id="field" value="" />
<input type="submit" value="Send to AJAX" />
</form>
<div id="success"> </div>
</html>

和我的节目.php它以id="成功"显示数据

<?php
// Process form data
echo '<strong>You submitted to me:</strong><br/>';
print_r( $_REQUEST );
?>

请帮忙...

首先你必须在jquery之后加载jquery ui

<script src="style/jquery-1.11.1.min.js"></script> 
<script src="style/jquery-ui.js" type="text/javascript" charset="utf-8"></script>

那么在这种情况下,你不需要使用live,你可以简单地这样做

$("#ajaxquery").submit(function(){
    // your code
})

"live"函数(或现在的"on"),用于在设置事件后创建或加载HTML代码。