JSON返回的数据这个应该只显示abc啊 为什么提交后把原来的页面的内容又复制了一份显示出来?这样就出现两个文本框和两个按钮了 这是为什么?

<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="ajax.js">
</script>
<script type="text/javascript">
window.onload=function(){
var oIpt1=document.getElementById('ipt1');
var oBtn1=document.getElementById('btn1');
var oShow=document.getElementById('show');
oBtn1.onclick=function(){
oV1=oIpt1.value;
ajax("test2.php",oV1,function(str){
oShow.innerHTML=str;
})
}
}
</script>
</head>
<body>
<input type="text" id="ipt1">
<button id="btn1">提交</button>
<p id="show">
</p>
<?php
if(!empty($_POST['test'])){
$a='abc';
$json=json_encode($a);
echo $json;
}
?>
</body>
</html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你混用了 ajax。因为你的代码:
此时的
str是整个 html 文件啊。你请求的是 test2.php,而 test2.php 的内容是:
因此ajax执行完后,<p id="show"></p> 的内容会把上面的都插入进去。
你应该单独写一个 PHP 文件,只输出文本框的内容,ajax.php
这样就可以了。