简体   繁体   English

jQuery动态元素删除所有输入字段

[英]Jquery dynamic elements remove all input fields

I am working on a form that can handle dynamically added fields, it also styles them according to mobile jquery. 我正在研究一种可以处理动态添加的字段的表单,它也可以根据移动jquery设置样式。

But my problem is that when I set up the remove it removes all the input fields. 但是我的问题是,当我设置删除时,它会删除所有输入字段。

var max_fields      = 10; //maximum input boxes allowed
var wrapper         = $(".input_fields_wrap"); //Fields wrapper
var add_button      = $(".add_field_button"); //Add button ID
var x = 1; //initial text box count

$(add_button).click(function(e){ //on add input button click
    e.preventDefault();
    if(x < max_fields){ //max input box allowed
    x++; //text box increment
    $('<div class="ui-grid-a">'+
      '<div class="ui-block-a">'+
        '<div data-role="fieldcontain">'+
          '<label for="hozzavalo">Hozzavalo: </label>'+
          '<input name="hozzavalo[]" id="hozzavalo" type="text">'+
        '</div>'+
      '</div>'+
      '<div class="ui-block-b">'+
        '<div data-role="fieldcontain">'+
          '<label for="mennyiseg">Mennyiseg: </label>'+
          '<input name="mennyiseg[]" type="number"> <span style="margin-left:10px;">lb</span>'+
        '</div>'+
      '</div>'+
      '</div><a href="#" class="remove_field">Remove</a>').appendTo('.input_fields_wrap');//add input box
      $(".input_fields_wrap").trigger("create");
    }
});

$(wrapper).on("click",".remove_field", function(e){
    e.preventDefault(); $(this).parent('div').remove(); x--;
})

I need the $(".input_fields_wrap").trigger("create"); 我需要$(".input_fields_wrap").trigger("create"); to style the fields, but when I apply it I lose all the fields when I try to remove one. 样式的字段,但是当我应用它时,当我尝试删除一个字段时,我丢失了所有字段。

 $(this).parent('div').remove();

this in this case is your .remove_field element. 在这种情况下,这是您的.remove_field元素。 With your code you remove the parent div of this element which is the whole container. 使用代码删除该元素的父div,即整个容器。 You have to either correctly nest the remove element so it just selects the corresponding field or fix your jquery selector. 您必须正确地嵌套remove元素,以便它仅选择相应的字段或修复jquery选择器。

To Remove current input fields data Change the structure HTML Add remove link inside the div . 删除当前输入字段数据更改结构div内的HTML Add remove链接。 HTML : HTML:

     <div class="ui-grid-a">
                       <div class="ui-block-a">
                         <div data-role="fieldcontain">
                           <label for="hozzavalo">Hozzavalo: </label>
                           <input name="hozzavalo[]" id="hozzavalo" type="text">
                         </div>
</div>
                       <div class="ui-block-b">
                         <div data-role="fieldcontain">
                           <label for="mennyiseg">Mennyiseg: </label>
                           <input name="mennyiseg[]" type="number"> <span style="margin-left:10px;">lb</span>
                         </div>
                       </div>
                      <a href="#" class="remove_field">Remove</a>
                       </div>


 Jquery Section:


     $(document).on("click",".remove_field", function(e){
                    e.preventDefault(); 
                $(this).parent().remove(); 
             })

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

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