2011-04-12 43 views
3

我有这个网站jQuery的各功能不能正常工作需要更换每个属性

<form method="POST" action="" id="order" class="forms"> 
    <div> 
     <span class="col1">Ref.</span> 
     <span class="col2">Number</span> 
     <span class="col3">Color</span> 
     <span class="col4">Remarks</span> 
     <span class="col5">Prijs</span> 
    </div> 
    <div class="order"> 
     <input type="text" name="ref-1" class="col1" /> 
     <input type="text" name="num-1" class="col2" /> 
     <input type="text" name="color-1" class="col3" /> 
     <input type="text" name="remark-1" class="col4" /> 
     <input type="text" name="price-1" class="col5" /> 
    </div> 
    <a id="add" href="#">+</a> 
</form> 

而且这个jQuery

$("#add").click(function() {  
    $("#order").children(":eq(1)").clone().each(function() { 
      var index = $("#order").children().length - 1; 
      alert(index); 

     $(this).children().find(":input").each(function() { 

       alert(index); 

        $(this).attr("[name]", function(i, v) { 

         return v.substr(0, v.lastIndexOf("-") + 1) + index; 

        }).val(""); 

       }); 
    }).insertBefore("#add"); 
}); 

我需要每个输入名称的一部分-1替换增加后的值。 但JS不会继续执行$(this).attr()功能 当我运行这段代码并不显示第二个警告框,当我点击#add

回答

3

我没有检查了整个代码的,但有一个明显的错误这

.attr("[name]" 

应该

.attr("name" 

您还需要去掉.children()从线

$(this).children().find(":input") 

并使其

$(this).find(":input") 

这是因为this是指包含输入的div元件。通过运行.children(),它将返回input元素,并且通过执行find(),因为它不返回任何内容,因为find会查找选定元素内部,而不包括它在其上运行的元素。

更新的jsfiddlehttp://jsfiddle.net/gaby/TpmdP/

+0

这并没有解决我的问题,但你是对的。 – as3student 2011-04-12 10:06:18

+0

@ as3student,用解决方案更新了答案。 – 2011-04-12 10:10:17

+0

你测试过了吗?因为现在我得到第二个警报,但它不会添加我克隆的元素。这真的很奇怪 – as3student 2011-04-12 11:28:50

0

删除.find( “:输入”),并通过 “:输入” 作为参数。儿童()。 children()可以通过过滤元素来检索子元素。