我发布的脚本工作正常,它只是在页面加载时在后台执行我的php代码。我不能做的是在按下提交按钮后在后台启动 php。我试图在正文中添加一个表单,但我不知道如何将其与 ajax 链接。怎么可能呢?谢谢!
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url: 'trial.php',
beforeSend: function() {
// you could also put a spinner img or whatever here too..
$("#myStatusDiv").html("started..");
},
success: function(result) {
$("#myStatusDiv").html(result);
}
});
});
</script>
</head>
<body>
</body>
</body>
</html>
您当前正在准备加载文档时执行代码。应将此代码添加到按钮单击事件中。
所以取而代之的是:
$(document).ready(function() {
//your code
});
这样做:
$(document).ready(function() {
$('#buttonid').click(function() {
//your code
});
});