jquery点击事件不断解除绑定(bind)

标签 jquery

当我运行代码时,点击事件第一次起作用。当我多次单击它时,似乎单击次数是无限的。如何保持点击事件有界?

var Categories = function() {
        $("select[id^='CategoryListBox-']").click(
        function() {
            SelectedItem = $(this).val();
            $(this).parent().load('/Categories/CategoryPicker?CatID=' + SelectedItem);

            //send CatID to the requesting page
            $('form#SubmitPost').prepend('<input id="CatID" name="CatID" type="hidden" value="' + SelectedItem + '" />');
        });
    }

    Categories();

最佳答案

您通过替换父元素内的 dom 来覆盖点击处理程序。您可以使用 live 来始终保持绑定(bind)。

第 2 行是:

$("select[id^='CategoryListBox-']").live('点击',

这样就可以了

关于jquery点击事件不断解除绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2308435/

相关文章:

jquery - 如果少于 3 个,则将元素追加到 :eq(2) if there are 3 or more on the page, 或 :eq(1)

javascript - 使用 javascript/jquery 循环 json 对象

javascript - jQuery JIRA Post 无法在 IE/FF 中工作

javascript - fullpage.js - 从外部链接跳转到特定部分

javascript - 如何使用 Javascript 运行带有 on() 函数的 JS

jquery - 基本 jQuery 动画 : Elipsis (three dots sequentially appearing)

jquery - Jquery ui 自动完成文本框上的退格键按下不会删除上面的任何字母?

javascript - jQuery 正则表达式 - 无法将 img 标签的标题属性中的换行/换行替换为 <br>

javascript - 我想淡化跨度的边框和背景而不影响其在 jQuery 中的内容

javascript - 如何使用javascript延迟标题标签中的脚本?