停止使用 ajax 、javascript 、php 刷新


Stop refreshing with ajax , javascript , php

我的代码如下所示:

<html>
<body>
<script language="javascript" type="text/javascript">
function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!
        ajaxRequest = new XMLHttpRequest();
    var age = document.getElementById('age').value;
    var queryString = "?age=" + age;
    ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
    ajaxRequest.send(null); 
}
</script>
<form name='myForm'>
Max Age: <input type='text' id='age' /> <br />
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>
</body>
</html>

我使用 Ajax 停止重定向,但它仍然令人耳目一新,我该如何停止它?

您可以尝试添加返回假;

<input type='button' onclick='ajaxFunction();return false' value='Query MySQL' />

好吧,代码对我来说并不令人耳目一新,但我仍然不擅长 ajax 您是否检查了错误控制台或尝试了 jQuery,您可以使用它轻松完成

<script language="javascript" type="text/javascript">
function ajaxFunction(){
    var age = $("#age").val();
    $.get("ajax-example.php",{"age":age},function(r){ /*Do something with response*/ });
}
</script>
Max Age: <input type='text' id='age' /> <br />
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</body>
</html>