最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamically add and remove TextBoxes and get value of dynamic TextBox using jQuery - Stack Overflow

programmeradmin1浏览0评论

I have a problem. The code below is for adding and removing textbox dymically through .append() and .remove(). I want all the data inside the textboxes with a placeholder of textbox will be imploded and will be placed in the textbox I set with name of textbox. Also the ones with the placeholder of box will be placed in the allotted textbox for it. How to do that?

Here's my code:

HTML

<form class="form-horizontal" method= "Post">
    <div class="control-group">
        <div class="inc">
            <div class="controls">
                <input type="text" class="form-control" name="textbox" placeholder="textbox"/> 
                <input type="text" class="form-control" name="text" placeholder="text"/>
                <button style="margin-left: 50px" class="btn btn-info" type="submit" id="append" name="append">
                Add Textbox</button>
                <br>
                <br>
            </div>
        </div>
        <input type="submit" class="btn btn-info" name="submit" value="submit"/> 
        <input type="text" class="form-control" name="textbox" placeholder="texbox"/> 
    <input type="text" class="form-control" name="text" placeholder="text"/>
    </div>
</form>

JavasScript:

<script type="text/javascript">
    jQuery(document).ready( function () {
        $("#append").click( function() {
        $(".inc").append('
            <div class="controls">
                <input class="form-control" type="text" name="textbox" placeholder="textbox">
                <input class="form-control" type="text" name="text" placeholder="text">
                <a href="#" class="remove_this btn btn-danger">remove</a>
                <br>
                <br>
            </div>');
        return false;
        });

    jQuery('.remove_this').live('click', function() {
        jQuery(this).parent().remove();
        return false;
        });
    });
</script>

I have a problem. The code below is for adding and removing textbox dymically through .append() and .remove(). I want all the data inside the textboxes with a placeholder of textbox will be imploded and will be placed in the textbox I set with name of textbox. Also the ones with the placeholder of box will be placed in the allotted textbox for it. How to do that?

Here's my code:

HTML

<form class="form-horizontal" method= "Post">
    <div class="control-group">
        <div class="inc">
            <div class="controls">
                <input type="text" class="form-control" name="textbox" placeholder="textbox"/> 
                <input type="text" class="form-control" name="text" placeholder="text"/>
                <button style="margin-left: 50px" class="btn btn-info" type="submit" id="append" name="append">
                Add Textbox</button>
                <br>
                <br>
            </div>
        </div>
        <input type="submit" class="btn btn-info" name="submit" value="submit"/> 
        <input type="text" class="form-control" name="textbox" placeholder="texbox"/> 
    <input type="text" class="form-control" name="text" placeholder="text"/>
    </div>
</form>

JavasScript:

<script type="text/javascript">
    jQuery(document).ready( function () {
        $("#append").click( function() {
        $(".inc").append('
            <div class="controls">
                <input class="form-control" type="text" name="textbox" placeholder="textbox">
                <input class="form-control" type="text" name="text" placeholder="text">
                <a href="#" class="remove_this btn btn-danger">remove</a>
                <br>
                <br>
            </div>');
        return false;
        });

    jQuery('.remove_this').live('click', function() {
        jQuery(this).parent().remove();
        return false;
        });
    });
</script>
Share Improve this question edited Jan 12, 2016 at 16:48 Bogdan Bogdanov 1,7232 gold badges21 silver badges31 bronze badges asked Jan 12, 2016 at 16:19 Anecito Alima SantillanAnecito Alima Santillan 951 gold badge3 silver badges10 bronze badges 17
  • please, i submitted a question a bit similar to this but different approach in javascript. it's much more easy to study than the i han sent first,. – Anecito Alima Santillan Commented Jan 12, 2016 at 16:28
  • Possible duplicate of stackoverflow./questions/34746695/… – guest271314 Commented Jan 12, 2016 at 16:30
  • yeah, its a bit the same but the javascript is different, in new to javascript, so i prefer this one than the first one i posted because its is easier to study. – Anecito Alima Santillan Commented Jan 12, 2016 at 16:42
  • Solution is same as stackoverflow./a/34746914 – guest271314 Commented Jan 12, 2016 at 16:43
  • but how can i double the textbox? thats what my problem is, in .append() i can just create another one, but there i can't understand it. – Anecito Alima Santillan Commented Jan 12, 2016 at 16:49
 |  Show 12 more ments

2 Answers 2

Reset to default 3

jQuery(document).ready( function () {
        $("#append").click( function(e) {
          e.preventDefault();
        $(".inc").append('<div class="controls">\
                <input class="form-control" type="text" name="textbox" placeholder="textbox">\
                <input class="form-control" type="text" name="text" placeholder="text">\
                <a href="#" class="remove_this btn btn-danger">remove</a>\
                <br>\
                <br>\
            </div>');
        return false;
        });

    jQuery(document).on('click', '.remove_this', function() {
        jQuery(this).parent().remove();
        return false;
        });
    $("input[type=submit]").click(function(e) {
      e.preventDefault();
      $(this).next("[name=textbox]")
      .val(
        $.map($(".inc :text"), function(el) {
          return el.value
        }).join(",\n")
      )
    })
  });
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<form class="form-horizontal" method= "Post">
    <div class="control-group">
        <div class="inc">
            <div class="controls">
                <input type="text" class="form-control" name="textbox" placeholder="textbox"/> 
                <input type="text" class="form-control" name="text" placeholder="text"/>
                <button style="margin-left: 50px" class="btn btn-info" type="submit" id="append" name="append">
                Add Textbox</button>
                <br>
                <br>
            </div>
        </div>
        <input type="submit" class="btn btn-info" name="submit" value="submit"/> 
        <input type="text" class="form-control" name="textbox" placeholder="texbox"/> 
    <input type="text" class="form-control" name="text" placeholder="text"/>
    </div>
</form>

jQuery(document).ready( function () {
        $("#append").click( function(e) {
          e.preventDefault();
        $(".inc").append('<div class="controls">\
                <input class="form-control" type="text" name="textbox" placeholder="textbox">\
                <input class="form-control" type="text" name="text" placeholder="text">\
                <a href="#" class="remove_this btn btn-danger">remove</a>\
                <br>\
                <br>\
            </div>');
        return false;
        });

    jQuery(document).on('click', '.remove_this', function() {
        jQuery(this).parent().remove();
        return false;
        });
    $("input[type=submit]").click(function(e) {
      e.preventDefault();
      $(this).next("[name=textbox]")
      .val(
        $.map($(".inc :text"), function(el) {
          return el.value
        }).join(",\n")
      )
    })
  });
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<form class="form-horizontal" method= "Post">
    <div class="control-group">
        <div class="inc">
            <div class="controls">
                <input type="text" class="form-control" name="textbox" placeholder="textbox"/> 
                <input type="text" class="form-control" name="text" placeholder="text"/>
                <button style="margin-left: 50px" class="btn btn-info" type="submit" id="append" name="append">
                Add Textbox</button>
                <br>
                <br>
            </div>
        </div>
        <input type="submit" class="btn btn-info" name="submit" value="submit"/> 
        <input type="text" class="form-control" name="textbox" placeholder="texbox"/> 
    <input type="text" class="form-control" name="text" placeholder="text"/>
    </div>
</form>

发布评论

评论列表(0)

  1. 暂无评论