当我按下链接时,程序应该转到 ajax 函数来更新数据库,而无需重新加载页面。 问题是当按下链接时没有进入 JS 函数(不显示警报)
<a href="#" onclick="xpto2()" >Remind later</a>
<script>
function xpto2(){
alert("hello");
$.ajax({
url: 'update-notify-status-noshow',
data: { postdata1: {{ n.id }} },
dataType: 'html',
type: 'get', )
success: function(output) {
alert(output);
}
});
</script>
最佳答案
修复你的 xpto2 函数。存在语法错误。这应该是这样的。
<script>
function xpto2() {
alert("hello");
$.ajax({
url: 'update-notify-status-noshow',
data: { postdata1: {{ n.id }} },
dataType: 'html',
type: 'get',
success: function(output) {
alert(output);
}
});
}
</script>
确保 jQuery 存在。
保持浏览器控制台打开以获得更好的见解。
这是一个可供尝试的完整模板。
更新
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
function xpto2() {
alert("hello");
$.ajax({
url: 'update-notify-status-noshow',
data: { postdata1: {{ n.id }} },
dataType: 'html',
type: 'get',
success: function(output) {
alert(output);
}
});
}
</script>
</head>
<body>
<a href="#" onclick="xpto2()" >Remind later</a>
</body>
</html>
关于javascript - Django Ajax 未进入函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30019045/