0

我有一个表格单元格,如下所示:

<td id=report onMouseUp='clickReport()' onKeyPress='clickReport()' >Click</td>";

事件函数如下:

function clickReport() {
document.form.submit();
}

在表单提交时,有一个后端过程正在进行。直到第一个进程完成(即,直到页面重新加载),我不希望用户再次按下“单击”,否则可能会影响之前的运行进程。所以,我想在第一次按下后禁用“点击”。

我尝试使用 preventDefault() 但它不起作用。

function clickReport() {
document.form.submit();
document.getElementById("report").onMouseUp = function(e) 
{
 e.preventDefault();
 return false;
}
document.getElementById("report").onKeyPress = function(e) 
{
 e.preventDefault();
 return false;
}
}

有人可以帮忙吗!

4

2 回答 2

1

1)您可以将元素参数传递给您的事件函数,这样您就可以轻松访问 DOM 元素。见下文。

<td id=report onMouseUp='clickReport(this)' onKeyPress='clickReport(this)' >Click</td>";

2)在第一个函数运行时,您可能会将事件设为空,因此它们将不再触发。见下文。

// the *element* parameter is yor <td> element here 
function clickReport(element) {
    document.form.submit();
    element.onMouseUp = null;
    element.onKeyPress= null;
}

3)如果你只想让它在点击时工作,你可以使用onclick事件而不是onmouseup并摆脱onkeypress 。

<td id=report onclick='clickReport(this)'>Click</td>";

function clickReport(element) {
    document.form.submit();
    element.onclick= null;
}

工作代码笔: https ://codepen.io/anon/pen/MmVNMe

于 2017-05-11T15:07:40.487 回答
0

这应该有效。

因为起初disableClick是未定义的,所以点击会触发,一旦触发,标志就会被设置为true,并且点击将不再可能。

<td id=report onMouseUp='!disableClick && clickReport()' 
 onKeyPress='!disableClick && clickReport()' >Click</td>"

function clickReport() {
 document.form.submit();
 window.disableClick = true;
}
于 2017-05-11T15:06:43.337 回答