2016-04-08 230 views
5

我有以下的html代码。它由Razor引擎生成。这是动态的。我的意思是,下拉的元素不会因用户而异。我需要将此下拉列表复制到同一个下拉列表中,并使用新的ID和新名称。我看了here,here,herehere。以下是我的html。我已在代码中添加注释以获得更多说明。如何更新div元素中html元素的id和名称?

<!-- the entire section with id and name template needs to be copied --> 
 
<div id="template" name="template" class="form-group unique"> 
 
    <label class="control-label col-md-2" for="Course">Course</label> 
 
    <!-- the drop down below has id and name as Course[0].Title 
 
I need to increment index inside [] --> 
 
    <div class="col-md-10"> 
 
    <select class="form-control drop valid" id="Course[0].Title" name="Course[0].Title"> 
 
     <option value="1">Statistics</option> 
 
     <option value="2">Trigonometry</option> 
 
     <option value="3">Biology</option> 
 
     <option value="4">Neurology</option> 
 
     <option value="5">Applied</option> 
 
     <option value="6">Theoretical</option> 
 
    </select> 
 
    </div> 
 
</div>

现在,我想出了下面的jQuery代码。它只复制一次,然后停止工作。我检查了控制台,没有错误。带有澄清意见的JavaScript代码如下。

< script src = "~/Scripts/jquery-1.10.2.min.js" > < /script> 
 
<script type="text/javascript 
 
"> 
 
    
 
    $(document).ready(function() { 
 
\t //copy the section with class name unique 
 
     var clone = $('.unique').clone(); 
 
\t //Copy again as i am modifying the id,name of div with unique class 
 
     var clone2 = $('.unique').clone(); 
 
\t //initialize the index with 1, the item with 0 index is already in webpage. 
 
     var i = 1; 
 
\t //Change name of drop down who has class drop and assign new name 
 
     $(".unique ").find(".drop ").attr(" 
 
name ", " 
 
Course[0].Title "); 
 
\t //Change id of drop down who has class drop and assign new id 
 
     $(".unique ").find(".drop ").attr(" 
 
id ", " 
 
Course[0].Title "); 
 
\t //on click of plus button, i add clone of drop down with parent div section and with new id and name 
 
     $("# 
 
plus ").click(function() { 
 
\t //Add after the element which has class unique 
 
      $('.unique').after(clone2); 
 
\t //Find name inside unique and update 
 
      $(".unique ").find(".drop ").attr(" 
 
name ", " 
 
Course[" + i + "].Title "); 
 
\t //Find id inside unique and update 
 
      $(".unique ").find(".drop ").attr(" 
 
id ", " 
 
Course[" + i + "].Title "); 
 
\t //Increment the index 
 
      i = i + 1; 
 
     }); 
 

 
    }); 
 
</script>

哪些错误的脚本?

回答

2

这就是你想要的吗?设置attr("name")attr("id")

时有一些错字错误也这里是我的代码来获取插入的作品,你要尽可能多:

我克隆元素在click()事件,使之工作。

$(document).ready(function() { 
//initialize the index with 1, the item with 0 index is already in webpage. 
    var i = 1; 
    $("#plus").on('click', function() { 
//Add after the element which has class unique 
     var clone2 = $('#template').clone(); 
     $("#template").after(clone2); 
     $(clone2).attr('id',"template"+i); 
     $(clone2).attr('name',"template"+i); 
//Find name inside unique and update 
     $(clone2).find(".drop ").attr("name", "Course[" + i + "].Title "); 
//Find id inside unique and update 
     $(clone2).find(".drop ").attr("id", "Course[" + i + "].Title "); 
//Increment the index 

     i++; 
    }); 
}); 

See this fiddle

+0

This Works。我没有在我的控制器中获得价值。可能还有更多东西要查找。我正在查。一旦我完成,我会接受答案。感谢努力。 –

+0

不客气。我还更改了在代码中克隆的'.unique'类的attr'id'和'name',因为ID必须是唯一的。 –

+0

是的。我注意到了。这十分完美。 –

0

尝试使用for loop先生

首先,设置例如一个div里面你选择:

<div class="select-inside"> 
    <select> 
    <option value="1">Statistics</option> 
    <option value="2">Trigonometry</option> 
    <option value="3">Biology</option> 
    <option value="4">Neurology</option> 
    <option value="5">Applied</option> 
    <option value="6">Theoretical</option> 
    </select> 
</div> 

在你的脚本,使用select元素存储到VAR jquery选择器。然后,使用for循环来克隆select元素并追加到文档中。

var select = $(".select-inside select"); 

for(var i = 0; i < numberOfYourChoice; i++) { 
    var clone = select; 
    clone.attr("id", "id[" + i + "]"); 
    clone.attr("name", "name[" + i + "]"); 
    document.append(clone); 
} 
+0

这是Razor(https://en.wikipedia.org/wiki/ASP.NET_Razor)生成的代码。我需要所有部分,而不仅仅是div,其中包括标签,带有id模板的外部div以及内部下拉。我需要根据用户选择进行复制。所以我怎么找到“numberOfYourChoice”? –

0

< script src = "~/Scripts/jquery-1.10.2.min.js" > < /script> 
 
<script type="text/javascript 
 
"> 
 
    
 
    $(document).ready(function() { 
 
\t //copy the section with class name unique 
 
     var clone = $('.unique').clone(); 
 
\t //Copy again as i am modifying the id,name of div with unique class 
 
     var clone2 = $('.unique').clone(); 
 
\t //initialize the index with 1, the item with 0 index is already in webpage. 
 
     var i = 1; 
 
\t //Change name of drop down who has class drop and assign new name 
 
     $(".unique ").find(".drop ").attr(" 
 
name ", " 
 
Course[0].Title "); 
 
\t //Change id of drop down who has class drop and assign new id 
 
     $(".unique ").find(".drop ").attr(" 
 
id ", " 
 
Course[0].Title "); 
 
\t //on click of plus button, i add clone of drop down with parent div section and with new id and name 
 
     $("# 
 
plus ").click(function() { 
 
\t //Add after the element which has class unique 
 
      $('.unique').after(clone2); 
 
\t //Find name inside unique and update 
 
      $(".unique ").find(".drop ").attr(" 
 
name ", " 
 
Course[" + i + "].Title "); 
 
\t //Find id inside unique and update 
 
      $(".unique ").find(".drop ").attr(" 
 
id ", " 
 
Course[" + i + "].Title "); 
 
\t //Increment the index 
 
      i = i + 1; 
 
     }); 
 

 
    }); 
 
</script>

1

你可以通过JS发布的数据,但它已被JS动态添加的元素不会返回到MVC的行动!

+0

感谢您的回答。尽管我解决了它... –

相关问题