简体   繁体   English

jQuery删除多个输入,然后仅追加一个

[英]jQuery remove multiple inputs then append just one

<input type="text" name="val1"/>
<input type="text" name="val2"/>

$('.beta-panel input').fadeOut(function(){
    $(this).remove();
    $('.beta-panel').append('<h1>Done</h1>');
});

I have the above code where when a button is clicked it runs a fade out and then appends and fades in a done tag. 我有上面的代码,当单击一个按钮时,它会淡出,然后在done标签中附加和淡入。 The problem is, when it fades and removes the inputs, it shows the same amount of <h1> tags as the inputs. 问题是,当它淡出并删除输入时,它会显示与输入相同数量的<h1>标签。

Instead of: 代替:

$('.beta-panel').append('<h1>Done</h1>');

do: 做:

$(this).closest('.beta-panel').append('<h1>Done</h1>');

$(this) holds reference to the clicked element and .closest will find the .beta-panel which is closest to $(this) and then it appends. $(this)保留对clicked元素的引用, .closest将找到最接近$(this) .closest .beta-panel ,然后追加。

You can use a .promise() in this case: 在这种情况下,您可以使用.promise()

$('.beta-panel input').fadeOut(function(){
   $(this).remove();
}).promise().done(function(){
   $('.beta-panel').append('<h1>Done</h1>');
});

From the documentation: 从文档中:

.promise()

Description: Return a Promise object to observe when all actions of a certain type bound to the collection, queued or not, have finished. 描述:返回一个Promise对象,以观察绑定到集合的某种类型的所有操作(是否排队)是否已完成。

Try something like : 尝试类似的东西:

$('.beta-panel input').fadeOut(function(){
    $(this).remove();

}).parent().append('<h1>Done</h1>');

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

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