简体   繁体   English

如何根据输入字段中的数字添加和删除HTML元素

[英]How to add and remove HTML elements depending on number in input field

I have a number input field in which a user can enter a number. 我有一个数字输入字段,用户可以在其中输入数字。 Depending on this number, some new input fields are generated. 根据此数字,将生成一些新的输入字段。

Currently I have no problems adding fields, as long as the new number put in by the user is higher than the previous. 目前我添加字段没有问题,只要用户输入的新号码高于前一个号码。 I can't remove the generated fields if the user puts in a lower number though. 如果用户输入较低的数字,我无法删除生成的字段。

So ie a user types in '5', which generates 5 new fields. 因此,即用户键入'5',这将生成5个新字段。 If the user adjusts this number to '3', there are still 5 generated fields. 如果用户将此数字调整为“3”,则仍有5个生成的字段。

Input HTML 输入HTML

<input type="number" class="form-control" name="amount_of_vote_groups" id="amount_of_vote_groups">

Javascript (jQuery) Javascript(jQuery)

var i = 1;
$('#amount_of_vote_groups').change(function () {
    while (i <= this.value) {
        $('#right-column').prepend(
                '<div class="form-group" id="generated-field-'+i+'">' +
                    // Some other stuff here
                '</div>'
        );
        i++;
    }
    if (this.value > i) {
        $('#right-column').remove('#generated-field-' + i);
    }
});

I know I'm doing something stupid with the remove statement, but I just can't it get figured out. 我知道我正在使用remove语句做一些愚蠢的事情,但我无法理解它。 I'm a real sucker at javascript ;) 我是javascript的真正傻瓜;)

I'd do it like this: 我这样做:

$('#amount_of_vote_groups').change(function() {
  $('#right-column div.form-group').remove();
  for (var i = 1; i <= this.value; i++) {
    $('#right-column').prepend(
      '<div class="form-group" id="generated-field-' + i + '">div</div>'
    );
  }
});

jsFiddle example jsFiddle例子

Whenever you change the number input, clear out any divs that may exist, then spin through a loop to add the new ones. 每当您更改数字输入时,清除可能存在的任何div,然后旋转循环以添加新的div。

The if loop should also be a while loop like this: if循环也应该像这样的while循环:

while (this.value > i) {
  $('#right-column').remove('#generated-field-' + i);
  i--;
}

In case someone stumbles along this question that wants to keep the generated forms already on the screen and only remove those that are greater than the requested number, here is an approach that uses jQuery's filter to get the excess form groups, then removes them. 如果有人偶然发现这个想要保留生成的表单已经在屏幕上并只删除那些大于请求数量的问题,这里有一种方法,它使用jQuery的过滤器来获取多余的表单组,然后删除它们。 It also moves index information to a data- attribute on the div to demonstrate how you might be able to expand on this to filter on other attributes without putting information into a div id. 它还将索引信息移动到div上的data-属性,以演示如何在此处展开​​以过滤其他属性,而无需将信息放入div id。

var currForms = 0;
$('#amount_of_vote_groups').change(function() {
  var numForms = this.value;
  if (currForms > numForms) {
    $('#right-column .form-group').filter(function(index, ele) {
        return $(ele).data('index') >= numForms;
    }).remove();
    currForms = numForms;
  } else {
    while (currForms < numForms) {
      $('#right-column').append(
        '<div class="form-group" data-index="' + currForms + '">' +
        'Field ' + currForms + ':<input type="text">' +
        '</div>'
      );
      currForms++;
    }
  }
});

Here's a fiddle . 这是一个小提琴 You can enter text in the generated inputs, and notice they remain for forms that are still on the page after entering in a lower number. 您可以在生成的输入中输入文本,并注意在输入较小的数字后仍保留在页面上仍然存在的表单中。

There is a solution depends on the difference of count between the i generated and the count of available elements, the class selector, this solution is assured from deleting the first element: 有一个解决方案取决于i生成的计数和可用元素的数量之间的差异,类选择器,这个解决方案可以保证删除第一个元素:

 var i = 1;
$('#amount_of_vote_groups').change(function () {
    while (i <= this.value) {
        $('#right-column').prepend(
                '<div class="form-group" id="generated-field-'+i+'">' +
                    '<input type="text" value="'+i+'" />'+
                '</div>'
        );
        i++
    }
  //alert($(this).val())
    if ((i-1) > $(this).val()) {

       // alert($('.form-group').size()+"***"+i)
      beDel = i - $('.form-group').size();
      for (j =0; j < beDel; j++){
        $('.form-group:first').remove();
      }
      --i;
    }

});

This is a DEMO 这是一个演示

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

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