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

javascript - Repeat a div when clicking a button - Stack Overflow

programmeradmin0浏览0评论

The below code is for repeating a div. But it does not work. Please help me.

<html>
  <head>
    <script>
      $(".repeat").live('click', function () {
        var $self = $(this);
        $self.after($self.parent().clone());
        $self.remove();
      });
  </script>
  </head>
  <body>
    <form>
      <div class="repeatable">
        <table border="1">
        <tr><td><input type="text" name="userInput[]"></td></tr></table>
        <button class="repeat">Add Another</button>
      </div>
    <input type="submit" value="Submit">
    </form>
  </body>
</html>

The below code is for repeating a div. But it does not work. Please help me.

<html>
  <head>
    <script>
      $(".repeat").live('click', function () {
        var $self = $(this);
        $self.after($self.parent().clone());
        $self.remove();
      });
  </script>
  </head>
  <body>
    <form>
      <div class="repeatable">
        <table border="1">
        <tr><td><input type="text" name="userInput[]"></td></tr></table>
        <button class="repeat">Add Another</button>
      </div>
    <input type="submit" value="Submit">
    </form>
  </body>
</html>
Share Improve this question edited Jan 15, 2014 at 11:00 Dhanu Gurung 8,85811 gold badges49 silver badges60 bronze badges asked Jan 15, 2014 at 10:56 user3189828user3189828 1851 gold badge5 silver badges16 bronze badges 3
  • 2 you miss jquery including – Nikita U. Commented Jan 15, 2014 at 10:57
  • @ Ushakov Nik... i included but it does not work – user3189828 Commented Jan 15, 2014 at 11:00
  • I dont know if it was your intent but you code is repeating a div inside it's cloned source: a div .repeatable inside another .repeatable. If you want them to be the same hierarchy level try: var parent = $self.parent(); parent.after(parent.clone());. – DontVoteMeDown Commented Jan 15, 2014 at 11:02
Add a ment  | 

4 Answers 4

Reset to default 5

First add jquery in your code and use on() in place of live().

Also write your code in $(function(){...}) so, that your code will work after document ready

<script src="http:/code.jquery./jquery-1.9.1.js"></script>
<script>
   $(function () {
       $(".repeat").on('click', function () {
          var $self = $(this);
          $self.after($self.parent().clone());
          $self.remove();
       });
   });
</script>

Working Demo

Updated, if you want it to work for more inputs then try this,

$(function () {
    $(".repeat").on('click', function (e) {
        e.preventDefault();// to prevent form submit
        var $self = $(this);
        $self.before($self.prev('table').clone());// use prev() not parent()
        //$self.remove();// remove this line so you can add more inputs
    });
});

Updated Demo

The $(".repeat") construction says that jQuery must be used. Try to add <script type="text/javascript" src="http://code.jquery./jquery-1.10.2.min.js"></script> before your <script>

Try this

HTML

<body>
    <form>
        <div class="parent">
      <div class="repeatable">
        <table border="1">
        <tr><td><input type="text" name="userInput[]"></td></tr></table>
        <button class="repeat">Add Another</button>
      </div>
      </div>
    <input type="submit" value="Submit">
    </form>
  </body>

Jquery

$(document).on('click', '.repeat', function (e) {
    e.preventDefault();
    $('.repeatable').parent('div.parent').append($('.parent').children('div:first').html());
});

See jsfiddle,

use

$(document).on('click', '.repeat', function () {

instead of

$(".repeat").live('click', function () {
发布评论

评论列表(0)

  1. 暂无评论