当您单击标签时,我正在尝试使用ajax将"测试"打印到屏幕上,但由于某种原因,它不起作用。我做错了什么?
test.php
<style>
#output {
width: 25%;
height: 25%;
border: 1px solid black;
}
</style>
<label value='show time' onclick="ajaxFunc('test1.php', 'output')"> Click me </label>
<div id='output'></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
function ajaxFunc(gotoUrl, output) {
$.ajax({
type: "POST",
url: gotoUrl,
error: function(xhr, status, error){
alert(error);
},
success: function(data) {
document.getElementById( output ).innerHTML = data;
} //end of success:function(data)
}); //end of $.ajax
</script>
test1.php
<?php
echo "testing";
?>
您应该得到一个
输入意外结束
错误,因为您没有关闭函数的大括号。
使用这个,
function ajaxFunc(gotoUrl, output) {
$.ajax({
type: "POST",
url: gotoUrl,
error: function(xhr, status, error) {
alert(error);
},
success: function(data) {
document.getElementById(output).innerHTML = data;
} //end of success:function(data)
});
}
请注意函数是如何用}
关闭的。
附加信息
一致性是关键,由于嵌套的事件侦听器,您在HTML中使用单引号和双引号,您应该在JavaScript中附加侦听器。
使用Chrome访问控制台时,请使用F12查看是否发生了任何错误。查看这个非常有用的链接,了解如何在其他浏览器中打开控制台。