jQuery - 可以删除与列表项关联的输入条目吗?

发布于 2024-12-29 20:07:12 字数 1925 浏览 2 评论 0原文

背景故事:我创建了一个工具,用于输入并按 Enter 键以根据需要向会员注册添加尽可能多的电子邮件地址。电子邮件作为列表项添加到可滚动的 div 中,每封电子邮件的右侧都有交替的斑马条纹和红色 x,允许用户删除他们希望在列表中删除的特定电子邮件。

事情是这样的:实际的输入是隐藏的,并且与该列表表单分开。列表形式实际上只是为了用户体验的观看乐趣/方便。

问题:我已成功定位列表项以删除我希望删除的内容,但这不会删除与列表项关联的输入。我如何定位这个单独的列表项?

这是我创建的基本代码:

目标并成功删除与红色 x 关联的最接近的列表项

$(".RemoveEmailBtn").live('click', function(e) {
    $(this).closest("li").fadeOut(300, function() { 
        $(this).remove();    
    });

// The additional code that is creating the list and inputs to give you a better idea of what I'm saying

} else {
    $('#Members').append('<li class="EmailList">' + email + '<span class="RemoveEmailBtn">x</span>' + '</li>');                                         
    $("li.EmailList:odd").addClass("oddItem");                     
    $('#add_email_field').append('<input class="email_inputs" type="hidden" name="emails" value="' + email + '"/>');
    $('#EmailInput').val('');
}

// The HTML as well: The HTML works perfectly. Again, the only issue is when I hit delete it only deletes the list item and not the hidden input
<div id="add_email_entry"><!-- add_email_entry -->
    <h2>Enter E-mail Addresses for New Members below:</h2>
    <div id="add_email_field"><!-- add_email_field -->
        <fieldset>
            <input class="xlarge" id="EmailInput" name="emailInput" size="30" type="text"/>
            <button id="AddEmailBtn" class="btn primary">Add</button>   
                <br/>
            <ul class="member" size="5" multiple="multiple" name="multiSelect" id="Members">
            </ul>
        </fieldset>
    </div>
    <!-- /add_email_field -->
</div>
<!-- /add_email_entry -->

现在如何删除列表项和输入,以便当我点击红色 X 时,它会删除两者而不仅仅是列表物品?输入一直存在,因为我无法弄清楚如何定位与我已经弄清楚如何删除的列表项相关的每个输入。

提前致谢!

Backstory:I've created a tool to type in and press enter to add as many email addresses to a member signup as necessary. The emails are added into a scrollable div as list items with an alternating zebra stripe and a red x to the right of each email allowing the user to delete the specific email they wish to in the list.

Here's the thing: the actual inputs are hidden and separate from this list form. The list form is really just for the viewing pleasure/ease of the UX.

Question: I have successfully targeted the list item to remove that I wish to but this does not remove the input associated with the list item. How would I target this separate list item?

Here is the basic code I've created:

Targets and successfully removes the closest list item associated with the red x

$(".RemoveEmailBtn").live('click', function(e) {
    $(this).closest("li").fadeOut(300, function() { 
        $(this).remove();    
    });

// The additional code that is creating the list and inputs to give you a better idea of what I'm saying

} else {
    $('#Members').append('<li class="EmailList">' + email + '<span class="RemoveEmailBtn">x</span>' + '</li>');                                         
    $("li.EmailList:odd").addClass("oddItem");                     
    $('#add_email_field').append('<input class="email_inputs" type="hidden" name="emails" value="' + email + '"/>');
    $('#EmailInput').val('');
}

// The HTML as well: The HTML works perfectly. Again, the only issue is when I hit delete it only deletes the list item and not the hidden input
<div id="add_email_entry"><!-- add_email_entry -->
    <h2>Enter E-mail Addresses for New Members below:</h2>
    <div id="add_email_field"><!-- add_email_field -->
        <fieldset>
            <input class="xlarge" id="EmailInput" name="emailInput" size="30" type="text"/>
            <button id="AddEmailBtn" class="btn primary">Add</button>   
                <br/>
            <ul class="member" size="5" multiple="multiple" name="multiSelect" id="Members">
            </ul>
        </fieldset>
    </div>
    <!-- /add_email_field -->
</div>
<!-- /add_email_entry -->

Now how do I remove both the list item AND the input so when I hit the red X it removes both and not just the list item? The inputs are sticking around because I can't figure out how to target each input related to the list item I have figured out how to delete.

Thanks in advance!

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

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

发布评论

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

评论(2

深海不蓝 2025-01-05 20:07:12

您必须找到类 .email_inputs 的输入,以及与您刚刚从列表中删除的电子邮件匹配的 value 属性。类似于:

$('.email_inputs[[email protected]]');

但是,如果您只是在创建的列表项中添加隐藏输入来显示电子邮件,那么只需删除 li 也会删除输入......

You'll have to find inputs with class .email_inputs, and the value attribute matching the email you just removed from the list. Something like:

$('.email_inputs[[email protected]]');

However, it would be much simpler if you just added the hidden inputs inside the list item you create to display the email, then just removing the li would remove the input too...

嗳卜坏 2025-01-05 20:07:12

您可以通过匹配其值来查找 input 元素来尝试此操作。

$(".RemoveEmailBtn").live('click', function(e) {
   $(this).closest("li").fadeOut(300, function() { 
       $(this).remove(); 
       var email = $(this).find(':not(.RemoveEmailBtn)').text();   
       $('input[value="' + email + '"]').remove();
   });
});

You can try this by finding the input element by matching its value.

$(".RemoveEmailBtn").live('click', function(e) {
   $(this).closest("li").fadeOut(300, function() { 
       $(this).remove(); 
       var email = $(this).find(':not(.RemoveEmailBtn)').text();   
       $('input[value="' + email + '"]').remove();
   });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文