PHP 响应逐渐消失
PHP response fades away
我正在尝试通过 ajax 获得响应,我的代码 (index.html) :
<button id="register">Register</button>
<p id="result">xxx</p>
<script>
$("#register").click(function(){
$.ajax({
url:'registration.php',
type: 'POST',
success:function(response){
$("#result").html(response)
}
})
})
</script>
和php (registration.php):
<?php
echo "yyy"
?>
我正在使用 xampp,我收到了回复,但它立即从页面上消失了。再次 xxx 出现在 p 标签内,有谁知道发生这种情况的原因是什么。
谢谢
看来,当您单击按钮以获取响应时,它还会刷新浏览器中的页面。您可以尝试以下方法来防止这种情况发生:
<script>
$("#register").click(function(evt) {
evt.preventDefault()
$.ajax({
url:'registration.php',
type: 'POST',
success: function (response) {
$("#result").html(response)
}
})
})
</script>
这会阻止您的浏览器执行您单击 <form>
内的 button.Any 按钮时正常执行的操作,标签将在当前 window 内自动发送 GET
请求,导致页面刷新。 preventDefault()
的另一种替代方法是在按钮上使用属性 type="button"
,这将使按钮不再是 type="submit"
按钮。
您可以在此处阅读有关我使用的函数的更多详细信息:
提交前阻止页面。
<input type='submit' id='register'>
<div id='result'></div>
$("#register").click(function(e){
e.preventDefault();
$.ajax({
url:'registration.php',
type: 'GET',
success:function(response){
document.getElementById("result").innerHTML = response;
}
})
});
我正在尝试通过 ajax 获得响应,我的代码 (index.html) :
<button id="register">Register</button>
<p id="result">xxx</p>
<script>
$("#register").click(function(){
$.ajax({
url:'registration.php',
type: 'POST',
success:function(response){
$("#result").html(response)
}
})
})
</script>
和php (registration.php):
<?php
echo "yyy"
?>
我正在使用 xampp,我收到了回复,但它立即从页面上消失了。再次 xxx 出现在 p 标签内,有谁知道发生这种情况的原因是什么。 谢谢
看来,当您单击按钮以获取响应时,它还会刷新浏览器中的页面。您可以尝试以下方法来防止这种情况发生:
<script>
$("#register").click(function(evt) {
evt.preventDefault()
$.ajax({
url:'registration.php',
type: 'POST',
success: function (response) {
$("#result").html(response)
}
})
})
</script>
这会阻止您的浏览器执行您单击 <form>
内的 button.Any 按钮时正常执行的操作,标签将在当前 window 内自动发送 GET
请求,导致页面刷新。 preventDefault()
的另一种替代方法是在按钮上使用属性 type="button"
,这将使按钮不再是 type="submit"
按钮。
您可以在此处阅读有关我使用的函数的更多详细信息:
提交前阻止页面。
<input type='submit' id='register'>
<div id='result'></div>
$("#register").click(function(e){
e.preventDefault();
$.ajax({
url:'registration.php',
type: 'GET',
success:function(response){
document.getElementById("result").innerHTML = response;
}
})
});