[英]Changing SELECT ID With Loop
I currently have a form that can add new entries on click using javascript. 我目前有一个可以在使用javascript单击时添加新条目的表单。 However, I would like to change the id of each subsequent "add-on": eg The first tbody has an id of "participant1", but the next one will have an id of "participant2", the eighth "participant8", and so forth.
但是,我想更改每个后续“附加组件”的ID:例如,第一个tbody的ID为“ participant1”,而下一个tbody的ID为“ participant2”,第八个为“ participant8”,并且等等。
Here is the excerpt from my main file: 这是我主文件的摘录:
<fieldset class="row2">
<legend>List of Participants</legend>
<p>
<input type="button" value="Add Participant" onclick="addRow('dataTable')" />
<input type="button" value="Clear Participants" onclick="deleteRow('dataTable')" />
<p>(All acions apply only to entries with check marked check boxes only.)</p>
</p>
<table id="dataTable" class="form" border="1">
<tbody>
<tr>
<p>
<td>
<input type="checkbox" required="required" name="chk[]" checked="checked" />
</td>
<td>
<div>Participant: </div>
<select name="participant1" id="participant1">
<option>Person A</option>
<option>Person B</option>
<option>Person C</option>
</select>
</td>
</p>
</tr>
</tbody>
</table>
<div class="clear"></div>
</fieldset>
And here is what I am currently attempting with my JS file: 这是我目前正在尝试使用JS文件的内容:
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if (rowCount < 50) {
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for (var i = 0; i < colCount; i++) {
var newcell = row.insertCell(i);
//newcell.id="participant"+ rowCount;
var cellBody = table.rows[0].cells[i].innerHTML;
newcell.innerHTML = replaceAll(cellBody, 'participant1', 'participant' + (rowCount + 1));
console.log(rowCount, newcell.innerHTML)
}
}
else {
alert("More than 50 Participants? Are you sure?");
}
}
function replaceAll(str, find, replace) {
var i = str.indexOf(find);
if (i > -1) {
str = str.replace(find, replace);
i = i + replace.length;
var st2 = str.substring(i);
if (st2.indexOf(find) > -1) {
str = str.substring(0, i) + replaceAll(st2, find, replace);
}
}
return str;
}
But this appears to only be taking in one participant? 但这似乎只招募一名参与者?
You are probably getting javascript errors. 您可能会遇到JavaScript错误。 May need to wrap the functions in some kind of dom-ready event.
可能需要在某种dom-ready事件中包装功能。
The fiddle here works, but only using jquery and its document ready check. 此处的小提琴有效,但仅使用jquery及其文档就绪检查。
http://jsfiddle.net/brettwgreen/17om5xpm/ http://jsfiddle.net/brettwgreen/17om5xpm/
I'd also recommend using (a) jquery and (b) knockout.js. 我还建议使用(a)jquery和(b)基因敲除(js)。
Once you start using knockout or similar JS framework for this kind of thing, you'll never go back. 一旦开始使用淘汰赛或类似的JS框架进行此类操作,您将永远不会回头。
JS: JS:
$(document).ready(function() {
window.addRow = function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 50){
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
console.log(colCount);
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
//newcell.id="participant"+ rowCount;
var cellBody = table.rows[0].cells[i].innerHTML;
newcell.innerHTML = replaceAll( cellBody, 'participant1' , 'participant' + (rowCount+1) );
console.log(rowCount, newcell.innerHTML)
}
}
else {
alert("More than 50 Participants? Are you sure?");
}
}
window.replaceAll = function replaceAll(str, find, replace) {
var i = str.indexOf(find);
if (i > -1) {
str = str.replace(find, replace);
i = i + replace.length;
var st2 = str.substring(i);
if(st2.indexOf(find) > -1) {
str = str.substring(0,i) + replaceAll(st2, find, replace);
}
}
return str;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.