如何通过Javascript删除输入

前端之家收集整理的这篇文章主要介绍了如何通过Javascript删除输入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
var i = 1;
$('.button').click(function() {
  $('<br/><input name="name' + (++i) + '" type="text"/>').insertBefore(this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
  <input name="name1" type="text" /><button class="button">+</button>
</div>

如何使用[]加号按钮放置一个按钮,当我点击它时.按钮删除该字段.

解决方法

你可以做这样的事情
var i = 1;
$('.button').click(function() {
  $('<br/><input name="name' + (++i) + '" type="text"/><button class="button1">-</button>').insertBefore(this);
});
$(document).on('click','.button1',function() {
  $(this)
    .prev('input').remove()
    //removing input field
    .end().prev('br').remove()
    //removing br tag
    .end().remove();
  //removing the button
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
  <input name="name1" type="text" />
  <button class="button">+</button>
</div>
原文链接:https://www.f2er.com/js/155297.html

猜你在找的JavaScript相关文章