Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击按钮时重复div_Javascript_Jquery_Html_Repeat - Fatal编程技术网

Javascript 单击按钮时重复div

Javascript 单击按钮时重复div,javascript,jquery,html,repeat,Javascript,Jquery,Html,Repeat,下面的代码用于重复div,但它不起作用。请帮帮我 <html> <head> <script> $(".repeat").live('click', function () { var $self = $(this); $self.after($self.parent().clone()); $self.remove(); }); </script> <

下面的代码用于重复div,但它不起作用。请帮帮我

<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>

$(“.repeat”).live('click',函数(){
var$self=$(本);
$self.after($self.parent().clone());
$self.remove();
});
再加一个

首先在您的
代码中添加
jquery
,并使用以代替

还要在
$(function(){…})
中编写代码,以便代码在
文档准备就绪后工作

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

首先在您的
代码中添加
jquery
,并使用以代替

还要在
$(function(){…})
中编写代码,以便代码在
文档准备就绪后工作

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

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

$(".repeat").live('click', function () {
使用

而不是

$(".repeat").live('click', function () {
$(“.repeat”)
结构表示必须使用jQuery。 尝试在

之前添加
$(“.repeat”)
构造说明必须使用jQuery。 尝试在您的

之前添加
,然后尝试此操作

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>
请参见,试试这个

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,包括@Ushakov Nik。。。我包括但它不起作用我不知道这是否是你的意图,但你的代码在它的克隆源中重复了一个div:一个div
。repeatable
在另一个
。repeatable
中。如果希望它们是相同的层次结构级别,请尝试:
var parent=$self.parent();parent.after(parent.clone())。repeatable
在另一个
。repeatable
中。如果希望它们是相同的层次结构级别,请尝试:
var parent=$self.parent();parent.after(parent.clone())。由:从jQuery 1.7开始,.delegate()已被该方法取代。因此,如果您同意,可以更新您的答案以提高其质量。由:从jQuery 1.7开始,.delegate()已被该方法取代。因此,如果您同意,您可以更新您的答案以提高其质量。在第二个div消失后,它只会重复两次。。。。每次我点击一个按钮时我都需要重复那个div,然后第二个div就消失了。。。。我需要在每次单击按钮时重复该div