[英]AJAX returns response but does not attach the HTML code from response to div
html :
<form method="POST">
<input type="text" name="unu" id="unu">
<input type="text" name="doi" id="doi">
<button type="button" onclick="check()">OK</button>
</form>
<div id="show_panels"></div>
jQuery的 :
function check() {
$.ajax({
type: "POST",
url: "2.php",
data: $("form").serialize(),
success: function(result) {
$('#show_panels').html(result);
},
error: function(result) {
console.log("Eroare:");
console.log(result);
}
});
var result_input = document.getElementById('result');
console.log(result_input.value);
}
的PHP :
<?php
$var_unu = $_POST['unu'];
$var_doi = $_POST['doi'];
$rezultat = $var_unu + $var_doi;
?>
<input type='text' value='<?php echo $rezultat ?>' id='result'/>
如您所见,我试图进入控制台(进行测试),返回带有id
结果的输入的值,但控制台返回:
Uncaught TypeError: Cannot read property 'value' of null
。
在页面源代码中,我没有Ajax返回输入的html代码。 我做错了什么?
浏览器视图:
查看页面源代码:
因为执行success
回调后存在HTML input
result
。
试试这个:
function check() {
$.ajax({
type: "POST",
url: "2.php",
data: $("form").serialize(),
success: function(result) {
$('#show_panels').html(result);
var result_input = document.getElementById('result');
console.log(result_input.value);
},
error: function(result) {
console.log("Eroare:");
console.log(result);
}
});
}
感谢@Titus的更正。
更新:
如果需要重用返回的值,则需要进行一些更改:
html :在表单中添加HTML input
result
。
<form method="POST">
<input type="text" name="unu" id="unu">
<input type="text" name="doi" id="doi">
<button type="button" onclick="check()">OK</button>
<input type="text" value="" id="result" />
</form>
<div id="show_panels"></div>
jquery :在HTML input
result
打印返回的值。
function check() {
$.ajax({
type: "POST",
url: "2.php",
data: $("form").serialize(),
success: function(result) {
$('#show_panels').html(result);
document.getElementById('result').value = result;
},
error: function(result) {
console.log("Eroare:");
console.log(result);
}
});
}
php :在PHP文件中,只需使用echo
返回$rezultat
。
<?php
$var_unu = $_POST['unu'];
$var_doi = $_POST['doi'];
$rezultat = $var_unu + $var_doi;
echo $rezultat;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.