最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery Dynamic Add Form Field, Remove Form Field - Stack Overflow

matteradmin5PV0评论

Hello I am trying to add new form field and delete form field after getting inspired from this tutorial -

My Current Problem is to delete and reset the value of all in chronological order.

<input type="hidden" name="count" value="1" />
<div class="control-group" id="fields">
           <label class="control-label" for="field1">Nice Multiple Form Fields</label>
           <div class="controls" id="profs"> 
             <div class="input-append">
               <input autoplete="off" class="span3" id="field1" name="prof1" type="text" placeholder="Type something (it has typeahead too)" data-provide="typeahead" data-items="8" 
data-source='["Aardvark","Beatlejuice","Capricorn","Deathmaul","Epic"]'/><button id="b1" onClick="addFormField()" class="btn btn-info" type="button">+</button>
             </div>
             <br /><small>Press + to add another form field :)</small>
           </div>
         </div>

Javascript :-

var next = 1;

function addFormField(){
    var addto = "#field" + next;
    next = next + 1;
    var newIn = '<br /><br /><input autoplete="off" class="span3" id="field' + next + '" name="field' + next + '" type="text" data-provide="typeahead" data-items="8"><button id="b1" onClick="$(this).parent().remove();" class="btn btn-info" type="button">+</button>';
    var newInput = $(newIn);
    $(addto).after(newInput);
    $("#field" + next).attr('data-source',$(addto).attr('data-source'));
    $("#count").val(next);  
}

Parent Element is getting removed, but next counter is not reset properly. in hidden and all new created form :-

Only Add Demo :-

Add an Delete Demo with Bug :- /

Can someone help me please.

Thanks

Hello I am trying to add new form field and delete form field after getting inspired from this tutorial - http://bootsnipp./snipps/dynamic-form-fields

My Current Problem is to delete and reset the value of all in chronological order.

<input type="hidden" name="count" value="1" />
<div class="control-group" id="fields">
           <label class="control-label" for="field1">Nice Multiple Form Fields</label>
           <div class="controls" id="profs"> 
             <div class="input-append">
               <input autoplete="off" class="span3" id="field1" name="prof1" type="text" placeholder="Type something (it has typeahead too)" data-provide="typeahead" data-items="8" 
data-source='["Aardvark","Beatlejuice","Capricorn","Deathmaul","Epic"]'/><button id="b1" onClick="addFormField()" class="btn btn-info" type="button">+</button>
             </div>
             <br /><small>Press + to add another form field :)</small>
           </div>
         </div>

Javascript :-

var next = 1;

function addFormField(){
    var addto = "#field" + next;
    next = next + 1;
    var newIn = '<br /><br /><input autoplete="off" class="span3" id="field' + next + '" name="field' + next + '" type="text" data-provide="typeahead" data-items="8"><button id="b1" onClick="$(this).parent().remove();" class="btn btn-info" type="button">+</button>';
    var newInput = $(newIn);
    $(addto).after(newInput);
    $("#field" + next).attr('data-source',$(addto).attr('data-source'));
    $("#count").val(next);  
}

Parent Element is getting removed, but next counter is not reset properly. in hidden and all new created form :-

Only Add Demo :- http://bootsnipp./snipps/dynamic-form-fields

Add an Delete Demo with Bug :- http://jsfiddle/6dCrT/2/

Can someone help me please.

Thanks

Share Improve this question edited Sep 23, 2013 at 6:36 Johny Bravo asked Sep 23, 2013 at 5:41 Johny BravoJohny Bravo 411 gold badge3 silver badges8 bronze badges 1
  • It seems, the only problem with jsfiddle/6dCrT/2 that when deleting does not removing <br>, resulting a lot <br>, thus leaves space before the last form input field. – klor Commented Jun 11, 2021 at 18:08
Add a ment  | 

2 Answers 2

Reset to default 1

Try:

function addFormField(){
    var addto = "#field" + next;
    next = next + 1;
    var newIn = '<br /><br /><input autoplete="off" class="span3" id="field' + next + '" name="field' + next + '" type="text" data-provide="typeahead" data-items="8"><button id="b'+next+'" onClick="$(this).prev().remove();$(this).remove();" class="btn btn-info" type="button">-</button>';
    var newInput = $(newIn);
    console.log(addto);
    $(addto).after(newInput);
    if(next>1)
        $("button#b"+next).after(newInput);
    $("#field" + next).attr('data-source',$(addto).attr('data-source'));
    $("#count").val(next);  
}

DEMO FIDDLE

In my example, I was building a form that would allow users to add multiple input names if they had more than one dog.

    var counter = 0
    jQuery(function () {
      $(".newDog").click(function(){
        counter ++;
        if (counter < 4) {
          var elem = $("<input/>",{
            type: "text",
            name: `dogname${counter}`
        });
        } else {
          alert("You can only add 4 Dog Names")
        }
        
        var removeLink = $("<span/>").html("X").click(function(){
            $(elem).remove();
            $(this).remove();
            counter --;
        });
        
        $(".dogname-inputs").append(elem).append(removeLink);
    });
    })

Full credit as mentioned to https://stackoverflow./users/714969/kevin-bowersox

Post a comment

comment list (0)

  1. No comments so far