javascript를 사용하여 테이블 행을 동적으로 추가하고 삭제하는 양식이 있습니다. 또한 각 행의 텍스트 상자와 각 행의 번호를 매기려고합니다. 내가 추가 한 네 개의 행이 있다고 가정 해 보겠습니다. 행 번호 3을 삭제하면 나머지 행은 1, 2, 4로 표시됩니다. jquery는 1, 2, 3 행의 번호를 다시 지정해야합니다. 내 코드는 아래에 게시됩니다. 행이 추가 된 후에 행이 인식되지 않는다는 직감이 있습니다. 아무도 이것으로 나를 도울 수 있습니까?Javascript가 클릭 이벤트에 응답하지 않는 행을 추가했습니다.
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function deleteRow(row) {
var x = document.getElementById('bom_table');
if (x.rows.length > 4) {
var i = row.parentNode.parentNode.rowIndex;
document.getElementById('bom_table').deleteRow(i);
}
}
function insRow() {
var x = document.getElementById('bom_table');
var len = x.rows.length;
// deep clone the targeted row
var new_row = x.rows[len - 2].cloneNode(true);
// get the total number of rows
// set the innerHTML of the first row
// new_row.cells[0].innerHTML = len - 2;
// grab the input from the first cell and update its ID and value
var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
inp1.id += len;
inp1.value = '';
// grab the input from the first cell and update its ID and value
var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
inp2.id += len;
inp2.value = '';
// grab the input from the first cell and update its ID and value
var inp3 = new_row.cells[3].getElementsByTagName('input')[0];
inp3.id += len;
inp3.value = '';
// grab the input from the first cell and update its ID and value
var inp4 = new_row.cells[4].getElementsByTagName('input')[0];
inp4.id += len;
inp4.value = '';
// grab the input from the first cell and update its ID and value
var inp5 = new_row.cells[5].getElementsByTagName('input')[0];
inp5.id += len;
inp5.value = '';
// append the new row to the table
var tbody = document.getElementById('bom_table').getElementsByTagName("tbody")[0];
tbody.appendChild(new_row);
}
function deleteRow2(row) {
var x = document.getElementById('ro_table');
if (x.rows.length > 4) {
var i = row.parentNode.parentNode.rowIndex;
document.getElementById('ro_table').deleteRow(i);
}
}
function insRow2() {
var x = document.getElementById('ro_table');
var len = x.rows.length;
// deep clone the targeted row
var new_row = x.rows[len - 2].cloneNode(true);
// get the total number of rows
// set the innerHTML of the first row
// new_row.cells[0].innerHTML = len - 2;
// if (len = 3)
// new_row = x.rows[2].cloneNode(true);
// else
// ;
// grab the input from the first cell and update its ID and value
var inp1 = new_row.cells[1].getElementsByTagName('input')[0];
inp1.id += len;
inp1.value = '';
// grab the input from the first cell and update its ID and value
var inp2 = new_row.cells[2].getElementsByTagName('input')[0];
inp2.id += len;
inp2.value = '';
// grab the input from the first cell and update its ID and value
var inp3 = new_row.cells[3].getElementsByTagName('input')[0];
inp3.id += len;
inp3.value = '';
// grab the input from the first cell and update its ID and value
var inp4 = new_row.cells[4].getElementsByTagName('input')[0];
inp4.id += len;
inp4.value = '';
// grab the input from the first cell and update its ID and value
var inp5 = new_row.cells[5].getElementsByTagName('input')[0];
inp5.id += len;
inp5.value = '';
// append the new row to the table
var tbody = document.getElementById('ro_table').getElementsByTagName("tbody")[0];
tbody.appendChild(new_row);
// x.appendChild(new_row);
}
</script>
<script type="text/javascript">
$(document).ready(function() {
var i = 0
var j = 1
var k = 1
$('input').each(function (i) {
$(this).attr("id", "text_" + i++);
})
$('.bom_rowcount').each(function (j) {
$(this).attr("innerHTML", 1 + j++);
})
$('.ro_rowcount').each(function (k) {
$(this).attr("innerHTML", 1 + k++);
})
$(".remove").click(removefunction());
function removefunction() {
$('input').each(function (i) {
$(this).attr("id", "text_" + i++);
})
$('.bom_rowcount').each(function (j) {
$(this).attr("innerHTML", 1 + j++);
})
$('.ro_rowcount').each(function (k) {
$(this).attr("innerHTML", 1 + k++);
})
};
$(".add").click(function() {
$('input').each(function (i) {
$(this).attr("id", "text_" + i++);
})
$('.bom_rowcount').each(function (j) {
$(this).attr("innerHTML", 1 + j++);
})
$('.ro_rowcount').each(function (k) {
$(this).attr("innerHTML", 1 + k++);
})
});
});
</script>
[jQuery : 동적으로 생성 된 요소의 이벤트 바인딩?] (http://stackoverflow.com/questions/203198/jquery-event-binding-on-dynamically-created-elements) – Esailija
거기에 * soooooo가 있습니다. 그래서 하나의 중복을 나열하는 것이 정의 정의를하지 않는 미래 요소들에 대한 jQuery 이벤트 바인딩에 관한 많은 질문들./하하 –