Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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 克隆表行,将随机或序列号附加到ID_Javascript_Jquery - Fatal编程技术网

Javascript 克隆表行,将随机或序列号附加到ID

Javascript 克隆表行,将随机或序列号附加到ID,javascript,jquery,Javascript,Jquery,所以我有这个密码 $('input.clone').live('click', function(){ //put jquery this context into a var var $btn = $(this); //use .closest() to navigate from the buttno to the closest row and clone it var $clonedRow = $btn.closest('tr').clone(); //ap

所以我有这个密码

$('input.clone').live('click', function(){
   //put jquery this context into a var
   var $btn = $(this);
   //use .closest() to navigate from the buttno to the closest row and clone it
   var $clonedRow = $btn.closest('tr').clone();
   //append the cloned row to end of the table

   //clean ids if you need to
   $clonedRow.find('*').andSelf().filter('[id]').each( function(){
       //clear id or change to something else
       this.id += '_clone';
   });

   //finally append new row to end of table
   $btn.closest('tbody').append( $clonedRow );
});
问题是,我克隆的每一行都会被命名为which\u clone 我该如何编写它,以便每次运行该函数时,它都会选择一个随机的或更理想的序列号来附加到“\u clone”的id isntead中


将以字符串形式为您提供一个介于0和999999之间的随机数。

为什么不使用表中的行数

$clonedRow.find('*').andSelf().filter('[id]').each( function(){
       //clear id or change to something else
       var rowCount = $btn.closest('tr').siblings().length;
       this.id += 'row_' + rowCount;
   });

身份证是从什么开始的?如果它们可以是row_x,那么当我从数据库中提取它们时会很有帮助,我将使用php为它们命名序列号。你确定只有tr才有id吗?否则,由于我的+size()比.length慢(size()实际上在内部调用了.length),所以这段代码将开始复制这个名称
$clonedRow.find('*').andSelf().filter('[id]').each( function(){
       //clear id or change to something else
       var rowCount = $btn.closest('tr').siblings().length;
       this.id += 'row_' + rowCount;
   });
$('input.clone').live('click', function(){
   var $btn = $(this);
   var $clonedRow = $btn.closest('tr').clone();

   $clonedRow.find('*').andSelf().filter('[id]').each( function(){
       this.id += '_clone' + $(this).siblings().size();
   });

   //finally append new row to end of table
   $btn.closest('tbody').append( $clonedRow );
});