我有下表实际上封装了动态表格。我需要在表的 Rmk 列中选择特定的 .check 类,并在Hello World
事件上为该行的任何输入字段打印blur()
。
在创建新行之前,它绝对可以正常工作。当我创建新行时,它无法选择该类。有什么帮助吗?
function myCreateFunction() {
var table = document.getElementById("myTable");
var c = document.getElementById("myTable").rows.length;
var row = table.insertRow(c);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = "<input type='text' class='lat' name='lat[]' /><span class='availability'></span>";
cell2.innerHTML = "<input type='text' class='long' name='long[]' /><span id='availability'></span>";
}
function myDeleteFunction() {
var cd = document.getElementById("myTable").rows.length;
if(cd<=3)
{
//do nothing
}
else{
document.getElementById("myTable").deleteRow(cd-1);
}
}
$('input').on('blur', function() {
var t = $(this);
t.closest('tr').find('.check').text('Hello World');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="form-handler.php" method="POST">
<table id="myTable">
<tr>
<th class="theader"> Lat </th>
<th class="theader"> Long </th>
<th class="theader"> Rmk </th>
</tr>
<tr>
<td class="troe"> <input type='text' class='lat' name='lat[]' /> </td>
<td class="trow"> <input type='text' class='long' name='long[]' /> </td>
<td class="trow">
<span class="check"></span>
</td>
</tr>
<tr>
<td class="troe"> <input type='text' class='lat' name='lat[]' /> </td>
<td class="trow"> <input type='text' class='long' name='long[]' /> </td>
<td class="trow">
<span class="check"></span>
</td>
</tr>
<tr>
<td class="troe"> <input type='text' class='lat' name='lat[]' /> </td>
<td class="trow"> <input type='text' class='long' name='long[]' /> </td>
<td class="trow">
<span class="check"></span>
</td>
</tr>
</table>
<button class="myBtn" type="submit" id='submit' name="submit" value="submit">submit</button>
</form>
<br/>
<button onclick="myCreateFunction()">Create New Row </button>
<button onclick="myDeleteFunction()">Delete Row</button>
答案 0 :(得分:0)
根据建议,答案的一部分与Event binding on dynamically created elements有关,我完全同意谁撰写评论以仔细阅读该评论以及有关事件委托的正式文档。
无论如何,这并不是完整的答案,因为在动态创建新行时需要更正代码。基本上,您不是在<span>
事件中使用带有类check
的{{1}}和类blur
创建第三个td元素。
最后,是这样的:
function myCreateFunction() {
var table = document.getElementById("myTable");
var c = document.getElementById("myTable").rows.length;
var row = table.insertRow(c);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = "<input type='text' class='lat' name='lat[]' /><span class='availability'></span>";
cell2.innerHTML = "<input type='text' class='long' name='long[]' /><span id='availability'></span>";
cell3.innerHTML = "<span class='check'></span>";
}
function myDeleteFunction() {
var cd = document.getElementById("myTable").rows.length;
if(cd<=3)
{
//do nothing
}
else{
document.getElementById("myTable").deleteRow(cd-1);
}
}
$(document).on('blur', '#myTable input', function() {
var t = $(this);
t.closest('tr').find('.check').text('Hello World');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="form-handler.php" method="POST">
<table id="myTable">
<tr>
<th class="theader"> Lat </th>
<th class="theader"> Long </th>
<th class="theader"> Rmk </th>
</tr>
<tr>
<td class="troe"> <input type='text' class='lat' name='lat[]' /> </td>
<td class="trow"> <input type='text' class='long' name='long[]' /> </td>
<td class="trow">
<span class="check"></span>
</td>
</tr>
<tr>
<td class="troe"> <input type='text' class='lat' name='lat[]' /> </td>
<td class="trow"> <input type='text' class='long' name='long[]' /> </td>
<td class="trow">
<span class="check"></span>
</td>
</tr>
<tr>
<td class="troe"> <input type='text' class='lat' name='lat[]' /> </td>
<td class="trow"> <input type='text' class='long' name='long[]' /> </td>
<td class="trow">
<span class="check"></span>
</td>
</tr>
</table>
<button class="myBtn" type="submit" id='submit' name="submit" value="submit">submit</button>
</form>
<br/>
<button onclick="myCreateFunction()">Create New Row </button>
<button onclick="myDeleteFunction()">Delete Row</button>
答案 1 :(得分:0)
我建议这样做要容易一些:
在行中添加一些隐藏的代码段,使其更易于使用。
<table id="table-row-source" style="display:none">
<tr>
<td class="troe"> <input type='text' class='lat' name='lat[]' /> </td>
<td class="trow"> <input type='text' class='long' name='long[]' /> </td>
<td class="trow">
<span class="check"></span>
</td>
</tr>
</table>
将您的myCreateFunction变成这样:
function myCreateFunction() {
$("#myTable").append($('#table-row-source').html());
InitializeInputEventBlur();
}
然后,我会将“模糊事件”处理程序转换为一个函数,该函数在准备好文档时以及每次创建新行以恢复绑定时都会调用。
function InitializeInputEventBlur() {
$('input').on('blur', function () {
var t = $(this);
t.closest('tr').find('.check').text('Hello World');
});
}
$(document).ready(function () {
InitializeInputEventBlur();
});