Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中第一次单击后禁用表单元格_Javascript_Onmouseup - Fatal编程技术网

在javascript中第一次单击后禁用表单元格

在javascript中第一次单击后禁用表单元格,javascript,onmouseup,Javascript,Onmouseup,我有一个表格单元格,如下所示: <td id=report onMouseUp='clickReport()' onKeyPress='clickReport()' >Click</td>"; 在表单提交方面,有一个后端流程正在进行。在第一个进程完成之前,也就是说,在页面重新加载之前,我不希望用户再次按下按钮,否则可能会影响上一个正在运行的进程。 因此,我想在第一次按下后禁用单击 我尝试使用preventDefault,但它不起作用 function clickRepo

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

<td id=report onMouseUp='clickReport()' onKeyPress='clickReport()' >Click</td>";
在表单提交方面,有一个后端流程正在进行。在第一个进程完成之前,也就是说,在页面重新加载之前,我不希望用户再次按下按钮,否则可能会影响上一个正在运行的进程。 因此,我想在第一次按下后禁用单击

我尝试使用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;
}
}
有人能帮忙吗

这应该行得通

由于disableClick最初是未定义的,因此单击将触发,一旦触发,该标志将设置为true,并且单击将不再可能

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

function clickReport() {
 document.form.submit();
 window.disableClick = true;
}
1您可以将元素参数传递给事件函数,以便轻松访问DOM元素。见下文

<td id=report onMouseUp='clickReport(this)' onKeyPress='clickReport(this)' >Click</td>";
// the *element* parameter is yor <td> element here 
function clickReport(element) {
    document.form.submit();
    element.onMouseUp = null;
    element.onKeyPress= null;
}
2在第一次运行函数时,您可能会使事件为空,这样它们就不会再触发了。见下文

<td id=report onMouseUp='clickReport(this)' onKeyPress='clickReport(this)' >Click</td>";
// the *element* parameter is yor <td> element here 
function clickReport(element) {
    document.form.submit();
    element.onMouseUp = null;
    element.onKeyPress= null;
}
3如果您只想让onclick事件在单击时工作,您可以使用onclick事件而不是onmouseup,并去掉onkeypress

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

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

工作代码笔:

如果您使用的是内联事件属性,您可以将它们设置为null:this.onMouseUp=null;使用addEventListener和RemoveEventListener将该值设置为null将禁用表格单元格,即使是第一次单击。。我在提交后做这件事。不知道为什么