【发布时间】:2016-01-01 22:19:02
【问题描述】:
我正在尝试使用 ajax 打印“Hello World!”,当您单击按钮时,由于某种原因它无法正常工作。我做错了什么?
test1.php
#testID {
border: 1px solid purple;
height: 50%;
width: 50%;
}
<?php
$commentID = "commentID";
$comment = "comment";
echo "
<input type = 'submit' value = 'Post' onclick = \"ajaxPass('test2.php', $comment, $commentID,'testID')\">
<div id = 'testID'></div>
";
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function ajaxPass(action,comment,commentID,outputID) {
$.ajax({
type: "POST",
url: action,
data: { comment: comment, commentID: commentID },
error: function(xhr,status,error){alert(error);},
success:function(data) {
document.getElementById(outputID).innerHTML = data;
} //end of success:function(data)
}); //end of $.ajax({
} //end of function ajaxPass(action,comment,commentID,outputID)
</script>
test2.php
<?php
echo "Hello World!";
?>
【问题讨论】:
-
您的 onclick 属性的语法错误。在你的网页上做一个查看源代码,看看它是如何输出的。
标签: javascript php jquery ajax