javascript - Django Ajax 未进入函数

标签 javascript jquery html ajax django

当我按下链接时,程序应该转到 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/

相关文章:

javascript - CSS 过渡持续时间不适用于过渡效果的第一个实例

javascript - 在javascript中选择菜单时创建一个新的div

javascript - js图像落入位置

html - 应用程序缓存 list

javascript - 读取动态更新的属性值jquery

javascript - 如何去掉javascript中最后一个逗号?

javascript - 仅针对主体禁用移动设备中的垂直滚动(也不适用于溢出 : auto) 的 div

jquery - 如何根据另一个输入框自动填充一个输入框

javascript - 列表框的双击事件

javascript - JS 单元测试 Mocha 中的方法链