Jquery表单问题。添加从选择框中删除选项

发布于 2024-11-01 09:40:21 字数 1640 浏览 0 评论 0原文

我有一个有很多选项的表格。

在表单中,我可以将数据设为私有或公开;

  1. 将数据设为私有:是、否(单选按钮)
  2. 如果单击“是”,则显示一个隐藏的 div,其中包含另外 2 个表单选项; 一个。会员姓名搜索(文字输入) b.选定的成员(多选择框)。

如果我单击将数据设为私有“是”,并搜索用户名,它会像建议框一样显示找到的结果。当我单击用户名(在建议框中显示的找到的结果之一中)时,我可以将其成功添加到我的多选择框(选定的成员)

如果我决定将其公开并在添加后单击将数据设为私有“否”一些成员到我的多选择框(选定的成员),我正在删除所有选项,并在其中添加 1 个选项(公共视图)。

问题从这里开始。在提交表单之前,如果我再次单击将数据设为私有“是”(1;单击是,选择了一些成员,2;单击否并从选择框中删除了所有选项,3;再次单击是),搜索一些成员并单击它们,它在我的多选择框中添加了两次(选定的成员)。

有趣的是,如果我执行两次(添加一些成员,单击“否”隐私和删除选项),下次当我想添加一些成员时,它会添加两次。如果我做5次,那就是加5次。

这是我的 Jquery;

// Private Options
$('input[name=make_private]').bind('click', function() {
    // Make It Private
    if ($(this).val() == 1) {
        $('.frm_make_private_1').fadeIn(1000);
        // Get clicked member
        $('a.member').live('click', function() {
            var username = $(this).text();
            var id = $(this).attr("id");
            // Add it to the Allowed Members Multi Selectbox
            $('#allowedMembers').append($("<option></option>").attr("value", id).text(username).attr("selected", "selected"));
            // Don't load the page
            return false;
        });
    }
    // Make It Public
    else {
        // Hide everything about Product Privacy
        $('.frm_make_private_1').fadeOut(1000);
        // Remove All Options
        $("#allowedMembers option").each(function() {
            $(this).remove();
        });
        // Add Public Option
        $('#allowedMembers').append($("<option></option>").attr("value", 0).text("Public View"));
    }
});

I have a form with a lot of options.

In the form, I can make the data private or public so;

  1. Make the data private : yes, no (radio buttons)
  2. If clicked yes, displaying a hidden div with 2 more form options;
    a. member name search (text input)
    b. selected members (multi selectbox).

If I click make the data private "yes", and search for a username, it is displaying found results like a suggestion box. When I click on a username (in one of the found results displaying in suggestion box), I can add it successfully to my multi selectbox (selected members)

If I decide to make it public and click make the data private "no" after adding some members to my multi selectbox (selected members), I'm deleting all options, and adding 1 option back in there (Public View).

The problem starts after here. Before submitting the form, if I click make the data private "yes" again, (1; clicked yes, selected some members, 2; clicked no and removed all options from selectbox, 3; clicked yes again), search for some members and click on them, it is adding 2 times in my multi selectbox (selected members).

Interestingly, if I do it 2 times (add some members, click no for privacy and delete options), next time when I want to add some members it is adding 2 times. If I do it 5 times, it is adding 5 times.

Here is my Jquery;

// Private Options
$('input[name=make_private]').bind('click', function() {
    // Make It Private
    if ($(this).val() == 1) {
        $('.frm_make_private_1').fadeIn(1000);
        // Get clicked member
        $('a.member').live('click', function() {
            var username = $(this).text();
            var id = $(this).attr("id");
            // Add it to the Allowed Members Multi Selectbox
            $('#allowedMembers').append($("<option></option>").attr("value", id).text(username).attr("selected", "selected"));
            // Don't load the page
            return false;
        });
    }
    // Make It Public
    else {
        // Hide everything about Product Privacy
        $('.frm_make_private_1').fadeOut(1000);
        // Remove All Options
        $("#allowedMembers option").each(function() {
            $(this).remove();
        });
        // Add Public Option
        $('#allowedMembers').append($("<option></option>").attr("value", 0).text("Public View"));
    }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺深海 2024-11-08 09:40:21

当您多次绑定事件时,您应该首先.unbind()。否则,您将有多个(可能是相同的)事件绑定到同一个操作

尝试

('input[name=make_private]').unbind().bind('click', function () {
// Make It Private
if ($(this).val() == 1) {
    $('.frm_make_private_1').fadeIn(1000);
    // Get clicked member
    $('a.member').unbind().live('click', function () {
        var username = $(this).text();
        var id = $(this).attr("id");
        // Add it to the Allowed Members Multi Selectbox
        $('#allowedMembers').append($("<option></option>").attr("value", id).text(username).attr("selected","selected"));
        // Don't load the page
        return false;
    });
}
// Make It Public
else {
    // Hide everything about Product Privacy
    $('.frm_make_private_1').fadeOut(1000);
    // Remove All Options
    $("#allowedMembers option").each(function() {
        $(this).remove();
    });         
    // Add Public Option
    $('#allowedMembers').append($("<option></option>").attr("value", 0).text("Public View"));
}
});

When you are binding events multiple time you should .unbind() first. Otherwise you will have multiple (and maybe same) events bound to the same action

Try

('input[name=make_private]').unbind().bind('click', function () {
// Make It Private
if ($(this).val() == 1) {
    $('.frm_make_private_1').fadeIn(1000);
    // Get clicked member
    $('a.member').unbind().live('click', function () {
        var username = $(this).text();
        var id = $(this).attr("id");
        // Add it to the Allowed Members Multi Selectbox
        $('#allowedMembers').append($("<option></option>").attr("value", id).text(username).attr("selected","selected"));
        // Don't load the page
        return false;
    });
}
// Make It Public
else {
    // Hide everything about Product Privacy
    $('.frm_make_private_1').fadeOut(1000);
    // Remove All Options
    $("#allowedMembers option").each(function() {
        $(this).remove();
    });         
    // Add Public Option
    $('#allowedMembers').append($("<option></option>").attr("value", 0).text("Public View"));
}
});
梦幻之岛 2024-11-08 09:40:21

找到了方法,将 $('a.member').live('click') 移到 $('input[name=make_private]').bind('click') 之外就可以了。谢谢罗恩指导我。

// Private Options
$('input[name=make_private]').bind('click', function () {
    // Make It Private
    if ($(this).val() == 1) {
        $('.frm_make_private_1').fadeIn(1000);
    }
    // Make It Public
    else {
        // Hide everything about Product Privacy
        $('.frm_make_private_1').fadeOut(1000);
        // Remove All Options
        $("#allowedMembers option").each(function() {
            $(this).remove();
        });         
        // Add Public Option
        $('#allowedMembers').append($("<option></option>").attr("value", 0).text("Public View"));
    }
});
// Get clicked member
    $('a.member').live('click', function () {
        var username = $(this).text();
        var id = $(this).attr("id");
        // Add it to the Allowed Members Multi Selectbox
        $('#allowedMembers').append($("<option></option>").attr("value", id).text(username).attr("selected","selected"));
        // Don't load the page
        return false;
    });

Found the way, moving $('a.member').live('click') outside of $('input[name=make_private]').bind('click') did the trick. Thank you Ron for guiding me.

// Private Options
$('input[name=make_private]').bind('click', function () {
    // Make It Private
    if ($(this).val() == 1) {
        $('.frm_make_private_1').fadeIn(1000);
    }
    // Make It Public
    else {
        // Hide everything about Product Privacy
        $('.frm_make_private_1').fadeOut(1000);
        // Remove All Options
        $("#allowedMembers option").each(function() {
            $(this).remove();
        });         
        // Add Public Option
        $('#allowedMembers').append($("<option></option>").attr("value", 0).text("Public View"));
    }
});
// Get clicked member
    $('a.member').live('click', function () {
        var username = $(this).text();
        var id = $(this).attr("id");
        // Add it to the Allowed Members Multi Selectbox
        $('#allowedMembers').append($("<option></option>").attr("value", id).text(username).attr("selected","selected"));
        // Don't load the page
        return false;
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文