javascript 如何在使用javascript验证一键后禁用按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17114825/
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
How to disable button after one click with validation using javascript?
提问by RGS
First I want to validate some of the field values entered or not. After that When i click the submit button, the button must be disabled after one click to avoid duplicate submission. How to do that using javascript?
首先,我想验证是否输入了一些字段值。之后,当我单击提交按钮时,必须在单击后禁用该按钮以避免重复提交。如何使用 javascript 做到这一点?
<script type="text/javascript" language="javascript">
function ValidateIt() {
if (document.getElementById('ddlProblemCategory').value == 0) {
alert("Please fill some value");
return false;
}
return true;
}
function DisableIt() {
if (ValidateIt() == true)
document.getElementById('btnSaveProblem').disabled = true;
}
</script>
回答by tymeJV
You can add an onclick
handler to your button:
您可以onclick
向按钮添加处理程序:
document.getElementById("idOfButton").onclick = function() {
//disable
this.disabled = true;
//do some validation stuff
}
回答by TechBytes
Call function submitbtn
onclick of the button.
submitbtn
单击按钮时调用函数。
Use
利用
function submitbtn(){
getElementById("Submit_id").disabled=true;
//Validation code goes here
}
回答by user2899989
Use hidden label and change its value on 1st click
使用隐藏标签并在第一次单击时更改其值
<script type = "text/javascript" language = "javascript">
function disableButton() {
var lblText = document.getElementById('lbl_hdn_text').innerHTML;
if (lblText == "true") {
document.getElementById('lbl_hdn_text').innerHTML = "false";
return true;
}
else {
return false;
}
}
</script>
<label id="lbl_hdn_text" style = "display:none;" >true</label>