簡體   English   中英

我想在我的每個輸入名稱屬性后附加計數器

[英]i want to append my each input name attribute with the counter

我的代碼將隱藏的div附加到按鈕單擊時的另一個div上,隱藏的div具有我想要的每個按鈕單擊名稱上的形式應具有一個計數器值

 var i=0; $(document).ready(function() { $("#addNew").click(function() { i++; var maindiv=document.getElementById('nestedFeilds').innerHTML; $("#nestedFeilds :input").each(function() { var orignalname=$(this).attr('name'); $(this).attr('name', orignalname + i); } ); $("#showhere").append(maindiv); } ); } ); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="row" id="mainContainer"> <div class="col-md-1 col-xs-4"> <label for="Education">Education</label> </div> <div class=class="col-md-8 col-xs-8 verticalLine" id="nestedFeilds" style=" margin-left: 10px ;float:left; display: none"> <a href="" style="color: red;margin-left: auto; ">Delete Education</a> <input type="text" placeholder="School Name" class="form-control" id="School_Name" required="" name="School_Name[]"> | <br> <input type="text" placeholder="Feild of Study" class="form-control" id="feild_Name" required="" name="feild_Name[]"> <br> <input type="text" placeholder="Degree" class="form-control" id="Degree_Name" required="" name="Degree_Name[]"> <br> </div> </div> <div class="row" id="2ndmainContainer"> <div class="col-md-1 col-xs-4"></div> <div class="col-md-8 col-xs-8 verticalLine" id="showhere" style=" margin-left: 10px ;float:left;"></div> <div style="margin-left: 120px; float:left;"><a id="addNew"> Add Education</a></div> </div> </form> </div> </div> <div class="col-xs-2"> </div> </div> </div> 

現在發生了什么事,我正在得到這樣的名稱屬性

School_Name_0
School_Name_01
School_Name_02

如果問題是如何獲取School_Name_0,School_Name_1,School_Name_2等,則只需在添加計數器值之前刪除字符串的最后一個字符即可:

$(this).attr('name', orignalname.substring(0, orignalname.length - 1) + i);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM