如何在使用jquery进行表克隆时自动增加onchange和onclick事件函数中的数字字段

时间:2013-09-07 10:37:37

标签: javascript jquery string clone alphanumeric

有人可以帮我解决这个问题:我有一个包含一行和多列的Maintable,在每个td /列中我有另一个使用id的不可用的东西。我基本上想要在最后一个td中克隆一个不可用的东西。

    <table id="innertable_0"
    <tr>
    <td class="policy_new_data  ">
    <div onclick="GetContent('familygender','0')" id="familygenderfirst_0"> 
    </div>
    <div id="familygendersecond_0">
       <select name="familygenderselect_0" onchange="changedropdownvalue('familyrelationselectField_0',this);showSelectedValue('familygender','0')" id="familygenderselectField_0"> 
          <option value="Male">Male</option>
          <option value="Female">Female</option>
       </select>
    </div>
    </td>
    </tr>
    </table>

这就是我克隆和覆盖Maintable的方式:

var $lasttdclone = $("#Maintable > tbody > tr:first > td:last").clone();

$lasttdclone.find('table').find('div').find('select').attr("id", function()
      {
         var parts = this.id.match(/(\D+)(\d+)$/);
         return parts[1] + ++parts[2];
       }).attr("name", function()
               {
                  var parts = this.name.match(/(\D+)(\d+)$/);
                  return parts[1] + ++parts[2];
                }).attr("onchange", function()
                       {
                            //what should be the logic to autoincremnt the numeric field by 1, in the onchange calling functions

                         });

    ($lasttdclone).appendTo("#Maintable > tbody > tr:first ") ;

我的问题是:我能够克隆表并能够将id和名称加1,但是我应该如何增加onchange和onclick函数中的数字。例如,我希望克隆表有  GetContent('familygender','1')而不是GetContent('familygender','0') changeropdownvalue('familyrelationselectField_1',this)而不是changedropdownvalue('familyrelationselectField_0',this)

我该怎么做?我尝试了regrex表达但未能达到理想的结果。请求帮助。谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

我得到了答案。

num=++part[2];
var fn=this.getAttributeNode('onchange').value;
return fn.replace(/(\d+)/g, num);

欢迎提出其他建议。

相关问题