。点击()事件执行仅在第一次事件

2023-09-10 18:22:25 作者:烟花再美只是瞬间

我的下一个脚本,/现金流/箭头/调用获取数据,并与它Django视图。然后,它返回一个在html加载的数据

I have the next script, "/cashflow/arrow/" calls a django view that gets the data and works with it. Then it returns the data that is loaded in the html.

该脚本工作正常,在第一次点击,但是当我想再次点击一个链接时,没有任何反应。

The script works fine in the first click, but when I want to click a link again, nothing happens.

<script>
$(document).ready(function(){   
    var prev_months = {{ prev_months }}
    var next_months = {{ meses_desp }}  

    function arrow(meses_ant, next_months) {
        $.get("/cashflow/arrow/", { prev_months: prev_months,   next_months: next_months},
          function(data){
            $("#cash_grid").html(data);      
         });
    }

    $("#dates_up").click( function() {
        if (prev_months > 0) {
            prev_months = prev_months - 1;
        }
        next_months = next_months + 1;
        arrow(prev_months, next_months);
    });
    $("#dates_down").click( function() {
        prev_months = prev_months + 1;
        if (next_months > 0) {
            next_months = next_months - 1;
        }
        arrow(prev_months, next_months);
    });

})

推荐答案

尝试使用 .live(点击,函数(){...}); 而不是click事件。 例如:

Try to use .live("click", function() {...}); instead the click event. Eg:

$("#dates_up").live("click", function()...
$("#dates_down").live("click", function()...

您可以阅读更多关于它这里

You can read more about it here