在我的代码中,我创建了一个JavaScript函数,它将从表中删除当前行。然后在HTML中我将该功能放入'输入'元素将触发' onclick'行动。如果我输入类型="按钮",一切正常,但是如果我输入="图像"正如您在下面看到的那样,即使使用"返回false;",每当我按下同一行的任何输入字段中的Enter时,它都会触发删除功能。
我不知道为什么。
function deleteRow(r)
{
var rowLength= document.getElementById("newOrder").rows.length; //get how many rows are in this table
if (rowLength == 2) //if there are only two rows (including header), then don't allow to delete a row
{
alert ("At least one row is needed to create an order.");
return;
} else //if row number is greater than 2, then delete a row is allowed
{
var i = r.parentNode.parentNode.rowIndex;
document.getElementById("newOrder").deleteRow(i);
}
以下是HTML代码
<tr>
<td class="item"><input type="image" onclick="deleteRow(this); return false;" src="img/delete.png" height="20" width="20" alt="delete"><input type="text" class="biginput, item" ></td>
<td class="detail"><input class="detail" type="number" ></td>
<td class="detail"><input class="detail" type="number" ></td>
</tr>
答案 0 :(得分:1)
使用此:Demo Link
<form>
<input id="image" onclick="return deleteRow(this);" type="image" src="img/delete.png" height="20" width="20" alt="delete" />
</form>
使用纯javascript:
function deleteRow(instance) {
alert("delete");
return false;
}
使用jquery,因为你在帖子中标记了jquery:
$('#image').click(function(event) {
event.preventDefault(); // yaa!
alert("delete");
});