如何通过Javascript删除输入

时间:2015-08-10 07:26:21

标签: javascript jquery html



var i = 1;
$('.button').click(function() {
  $('<br/><input name="name' + (++i) + '" type="text"/>').insertBefore(this);
});
&#13;
<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>
&#13;
&#13;
&#13;

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

4 个答案:

答案 0 :(得分:7)

只需在HTML字符串中添加一个新的按钮模板,然后将<div>包裹起来以识别。然后使用事件委托绑定到动态创建的删除按钮:

&#13;
&#13;
var i = 1;
$('.button').click(function() {
  // Wrap each row in a div. Won't need a <br> to create a line break and it's styleable
  $('<div><input name="name' + (++i) + '" type="text"/><button class="remove">-</button></div>').insertBefore(this);
});
// We need to use event delegation here, as the remove buttons are dynamically generated
$(document).on("click", ".remove", function() {
  // Remove the parent, which is the rows <div> element
  $(this).parent().remove();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="button">+</button>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

你可以做这样的事情

&#13;
&#13;
NSURL *movieURL = [NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"];  // sample url
MPMoviePlayerViewController *movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];

// Logic for play movie in landscape
CGAffineTransform landscapeTransform;
landscapeTransform = CGAffineTransformMakeRotation(90*M_PI/180.0f);
landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
[movieController.moviePlayer.view setTransform: landscapeTransform];

[self presentMoviePlayerViewControllerAnimated:movieController];
[movieController.moviePlayer prepareToPlay];
[movieController.moviePlayer play];
&#13;
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
});
&#13;
&#13;
&#13;

答案 2 :(得分:2)

parse_url

DEMO

答案 3 :(得分:1)

我强烈建议您使用Knockout,Angular或类似的东西。他们是MVVM模式的实现,这在你的场景中非常有用。

Here is a sample of how Knockout.js works, and directly correlates to your problem scenario.