我想,我已经设置好了,但它仍然不工作 - 我正在用 X $ c附加新的html元素 input $ c>删除标志。如果我点击新添加的 X 符号,应该删除新添加的输入。我做的是: $ b $ pre $ function addmore(){ $('< input type =filename ='bild []/>< a class =del> X< / a>< br />')。 ('click',function(){ alert('test'); }); }
我的html是:
< a href =javascript:addmore()>添加更多< / a> < p id =more>< / p>它插入的很好,但我无法删除新添加的元素(警报不会触发)。为什么是这样?
非常感谢解决方案
.find()函数在当前的匹配元素集合内查找元素,但不包含那些匹配的元素。您的< a class =del> 元素是匹配元素集合的一部分,因此不会被拾取。
使用 .filter() ('。del')。on('click',function(){}。 ...});
i think, i have all set, but it is still not working - i am appending new html element input with X delete sign. if i click on that newly added X sign, the newly added input should be deleted. what i did is:
function addmore(){ $('<input type="file" name="bild[]"/> <a class="del">X</a> <br/>').insertAfter('#more').find('.del').on('click', function(){ alert('test'); }); }my html is:
<a href="javascript:addmore()"> add more </a> <p id="more"></p>it is inserting well, but i cannot delete the newly added element (alert is not firing). why is this?
thanks a lot
解决方案The .find() function looks for elements that are inside the current set of matched elements, but doesn't include those matched elements. Your <a class="del"> element is part of the set of matched elements so won't be picked up.
Use .filter() instead:
....filter('.del').on('click', function() {...});
更多推荐
insertAfter
发布评论