Javascript html按钮在没有被禁用的情况下不可点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6899883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:40:46  来源:igfitidea点击:

html button not clickable without being disabled

javascripthtmlcssbutton

提问by andrew

Since IE (<8?) does not let me change the text color of a disabled button, and introduces a horrible shadow (embossed effect), I want to mimic the behaviour of being a disabled button. This button will be disabled if there are input errors on the form.

由于 IE (<8?) 不允许我更改禁用按钮的文本颜色,并引入了可怕的阴影(浮雕效果),我想模仿禁用按钮的行为。如果表单上有输入错误,此按钮将被禁用。

Instead of disabling I can change the class in JS to darken the button etc when form validation takes place. What I also want to do is make it unclickable as well so that the user can not submit the form. (How) can I do this?

在进行表单验证时,我可以更改 JS 中的类以将按钮等变暗,而不是禁用。我还想做的是使其不可点击,以便用户无法提交表单。(我怎样才能做到这一点?

Thanks

谢谢

回答by jusopi

IE10+ & Modern Browsers Answer

IE10+ 和现代浏览器答案

This won't solve your issuesince you're using < IE10, but for those who are using modern browsers and to answer the original question:

不会解决您的问题,因为您使用的是 < IE10,但对于那些使用现代浏览器并回答原始问题的人:

html button not clickable without being disabled

html按钮在没有被禁用的情况下不可点击

...this works simply & elegantly:

...这简单而优雅地工作:

.no-click {
    pointer-events: none;
}

Just add this class to your UI:

只需将此类添加到您的 UI 中:

<button class="no-click">

回答by mplungjan

<form onSubmit="return validate(this)"

Just return false to stop submission

只需返回 false 即可停止提交

you can add the function in the window.onload too:

您也可以在 window.onload 中添加该功能:

window.onload=function() {
  document.getElementsByTagName("form")[0]).onsubmit=function() {
    return validate(this);
  }
}
function validate(theForm) {
  // if not valid
  return false;

  // else
  return true;
}

If you want to adhere to the newest best practices, you will use addEventListener or attachEvent or jQuery bind

如果您想遵守最新的最佳实践,您将使用 addEventListener 或 attachEvent 或 jQuery bind

Comment From @BrendanEich:

来自@BrendanEich 的评论:

@mplungjan onclick of submit just falls out of that being a button; form onsubmit is clearly better.

@mplungjan onclick 的提交只是因为它是一个按钮;表单提交显然更好。

回答by T9b

Nothing is out of bounds in solving this. First of all re-enable your button, then using Jquery, addClass that you want to (you can also remove the old class too) then re-disable the button:

解决这个问题没有任何限制。首先重新启用您的按钮,然后使用 Jquery,添加您想要的类(您也可以删除旧类),然后重新禁用该按钮:

$("#yourbuttonid").click(function(){
    $(this).removeAttr("disabled").removeClass("classname").addClass("classname2").attr("disabled", "disabled");
});

回答by Barry Kaye

You need to return false from the onclick event:

您需要从 onclick 事件返回 false:

<input type="submit" value="Submit" onclick="return test();" />

or

或者

<input type="submit" value="Submit" onclick="return false;" />

NOTE: you must use returnin the onclick.

注意:您必须return在 onclick 中使用。