简体   繁体   English

消除跨度问题

[英]Removing span issue

I have a recipes application that looks like this: http://i.stack.imgur.com/FxL7R.png 我有一个食谱应用程序,看起来像这样: http : //i.stack.imgur.com/FxL7R.png

At the end of ingredient span is a delete button. 配料范围的末尾是一个删除按钮。 For some reason, my code is not working (which means its not deleting the ingredient span): 由于某种原因,我的代码无法正常工作(这意味着它没有删除成分范围):

HTML: HTML:

        <div class='formelementcontainer funky'>
        <label for="ingredient">Ingredients</label>
        <div id='ingredientsCOUNT' class='sortable'>
            <span>
                <input type="text" class='small' name="ingredient" id="ingredient" placeholder='QTY'/>
                <select name='measurements'>
                    <option value='' name='' checked='checked'>--</option>
                    <?foreach ($measurements as $m):?>
                        <option value='<?=$m->id;?>'><?=$m->measurement;?></option>
                    <?endforeach;?>
                </select>
                <input type="text" name="ingredient" id="ingredient" placeholder='Ingredient'/>
                <a class='float-right delete-button' id='deleteThis' style='margin:10px 2px;' href='#'><img src='<? echo base_url()."public/img/delete.png";?>' height='11' width='11' /></a>
            </span>
        </div>
        <div class="clear"></div>
        <div class='addSPAN tabover'>
            <a class='float-right' id='btnAddIngredients' href='#'>Add Ingredient</a>
        </div>
        </div>

JQUERY: JQUERY:

(function($) { 
$(document).ready(function () {
    $('#btnAddIngredients').click(function () {
        var num = $('#ingredientsCOUNT span').length;
        var newNum = new Number(num + 1);
        $('#ingredientsCOUNT > span:first')
            .clone()
            .attr('name', 'ingredient' + newNum)
            .appendTo('#ingredientsCOUNT')
            .fadeIn();
    });
    $('#deleteThis').click(function () {
        var span = $(this).closest('span');
        span.fadeOut('slow', function() { span.remove(); });
    });
});
})(jQuery);

What's supposed to happen is when I click on the X , the closest span (the containing span) is removed. 当我单击X时 ,应该发生的是,最接近的跨度(包含跨度)被删除了。 But when I click on it, nothing happens. 但是当我单击它时,什么也没有发生。 Any suggestions? 有什么建议么? Thanks for all help! 感谢您的帮助!


EDIT 编辑

Here is a JSFIDDLE: http://jsfiddle.net/sjDk7/1/ 这是一个JSFIDDLE: http : //jsfiddle.net/sjDk7/1/

The problem here is that you are attaching an event handler to elements that aren't yet in the document. 这里的问题是您将事件处理程序附加到文档中尚未存在的元素。 The easiest solution is to delegate event handling to an ancestor element using on : 最简单的解决方案是使用on将事件处理委托给祖先元素:

$('.formelementcontainer').on('click', '.deleteThis', function () {
    var span = $(this).closest('span');
    console.log(span);
    span.fadeOut('slow', function () {
        span.remove();
    });
});

You can see a demonstration here . 您可以在此处看到演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM