首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在克隆cloning内部的最后一个div之前,我需要更新name属性。

在克隆cloning内部的最后一个div之前,我需要更新name属性。
EN

Stack Overflow用户
提问于 2014-06-25 14:54:02
回答 3查看 73关注 0票数 0

在克隆cover内部的最后一个div之前,我需要更新name属性,这样就不会出现重复的名称。我该怎么做呢?我已尽我所能,只是在某一时刻堆了起来。如果你提供更好的JQ,我现在的JQ可能看起来很傻,很乐意改变它,因为我是JQ的新手。

谢谢

代码语言:javascript
复制
<div id="label-field-cover">
    <div><input type="text" value="" name="size-label-1"></input></div>
    <div><input type="text" value="" name="size-label-2"></input></div>
    <div><input type="text" value="" name="size-label-3"></input></div>
</div>

JQ

代码语言:javascript
复制
   //Clone last div
    var $cloned = $('#label-field-cover div:last').clone();

    //Get the content of cloned div
    var clonedContent = $cloned.html();

    //This echos: ""<input name="size-label-3" value="" type="text">""
    console.log(clonedContent);

    //I need to change name="size-label-3" to name="size-label-4" somewhere here first

    //This adds cloned element after last div
    $('#label-field-cover div:last').after($cloned);
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-06-25 15:29:13

由于您正在追加一个新元素,所以最后调用将选择该元素,您可以使用属性标记重命名它

代码语言:javascript
复制
//Clone last div
var $cloned = $('#label-field-cover div:last').clone();

//Get the content of cloned div
var clonedContent = $cloned.html();

//This echos: ""<input name="size-label-3" value="" type="text">""
console.log(clonedContent);

//I need to change name="size-label-3" to name="size-label-4" somewhere here first

//This adds cloned element after last div
$('#label-field-cover div:last').after($cloned);

//Add this in
$('#label-field-cover div:last input').attr("name", "size-label-4");

在这里添加$('#label-field-cover div:last input')输入值会使它将标签字段覆盖对象中的最后一个div元素的子输入元素作为目标。

票数 1
EN

Stack Overflow用户

发布于 2014-06-25 15:06:08

我认为您应该使用last()方法,而不是div:last,因为有了this。类似于:

代码语言:javascript
复制
var $cloned = $('#label-field-cover div').last().clone();

您想要实现的目标很简单,可以用jQuery实现:

代码语言:javascript
复制
var new_name = $cloned.children('input').attr('name', 'size-label-4');

jQuery很棒,所以您可以这样做:

代码语言:javascript
复制
$cloned.children('input').attr('name', 'size-label-4');
$('#label-field-cover div').last().after($cloned);

我希望它能成功,我没有测试它,xD

票数 3
EN

Stack Overflow用户

发布于 2014-06-25 15:17:39

给你:http://jsfiddle.net/vimes1984/4aXdV/22/

代码语言:javascript
复制
   //Clone last div
   var $cloned = $('#label-field-cover div:last').clone();

   //Get the content of cloned div
   var clonedContent = $cloned.html();

   //This adds cloned element after last div
   $('#label-field-cover div:last').after($cloned);
   //first we get the newly added attribute name
   var clonedattr = $('#label-field-cover div input:last').attr('name');
   //regex to match the diigits in it
   var clonednumber = clonedattr.match('([0-9]|[1-9][0-9]|[1-9][0-9][0-9])');
   //first digit returned we change from string to a actual number
   var getnumber = parseInt(clonednumber[0]);
   //add one to it   
   var addone = getnumber += 1;
   //check to make sure it's actually added   
   console.log(addone);
   //reapply to the last INPUT the attribute with the added number...
   $('#label-field-cover div:last input').attr('name', 'size-label-' + addone);

这将对最后一个添加的输入进行分级,并将其中一个添加到值中,然后将名称更改为新的附加值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24412048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档