jQuery 如何使用jquery禁用提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1237896/
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 submit button with jquery
提问by akano1
I have a simple form to check the value of post code entered by the user against a variable,
我有一个简单的表格来检查用户输入的邮政编码的值是否与变量相匹配,
I'm trying to disable the submit button and do the check and give alert message, I can't figure out what I'm doing wrong, any help would be appreciated.
我正在尝试禁用提交按钮并进行检查并发出警报消息,我无法弄清楚我做错了什么,任何帮助将不胜感激。
<form id="post_code">
<input name="textfield" type="text" id="postcode" size="14" maxlength="8" />
<input type="image" id="submit_postcode" src="images/B_go.png" alt="Submit Postcode" width="59" height="24" border="0" />
</form>
$(function() {
$('form#post_code').submit(function() {
var entered_post_code = $('form#post_code input#postcode').val();
var post_code = "CV";
if (entered_post_code == post_code) {
alert("post code is ok");
return true;
}else {
alert("post code is not ok");
return true;
}
return false;
});
});
});
回答by Mythica
The W3C recommends to set that attribute to disabled="disabled", so:
W3C 建议将该属性设置为 disabled="disabled",因此:
$('#submit_postcode').attr('disabled', 'disabled');
Re-enabling can be done by removing the attribute:
可以通过删除属性来重新启用:
$('#submit_postcode').removeAttr('disabled');
Setting the attribute to any value causes it to be disabled, even .attr('disabled', 'false'), so it has to be removed.
将属性设置为任何值都会导致它被禁用,即使是 .attr('disabled', 'false'),因此必须将其删除。
回答by David
$('#submit_postcode').attr('disabled', 'disabled');
回答by Quentin
If the check matches you return true, and if it doesn't match … you also return true. The return false
line is never reached.
如果检查匹配你返回真,如果它不匹配......你也返回真。这return false
条线永远不会到达。
You probably want to change one of your trues to false, and eliminate the line outside the if/else block.
您可能希望将其中一个 true 更改为 false,并消除 if/else 块之外的行。
回答by CarneyCode
This is the code I use to disable or re-enable a control:
这是我用来禁用或重新启用控件的代码:
function handleControlDisplay(className, foundCount, checked) {
var button = jQuery(className);
if (foundCount > 0 && foundCount == checked) {
// enable
button.removeAttr("disabled");
}
else {
// set the disabled attribute
button.attr("disabled", "true");
};
}
回答by Elzo Valugi
var entered_post_code = $('form#post_code input#postcode').val();
// rewrite as
var entered_post_code = $("#post_code input[id=postcode]").val();
You should return false if post code is not ok. no?
如果邮政编码不正确,您应该返回 false。不?
probably your if it never solves as true, and it returns as false always. The usage of submit() function is correct.
可能是你的,如果它永远不会解决为真,它总是返回为假。submit() 函数的用法是正确的。
回答by redsquare
//disable
$('#submit_postcode').attr('disabled', true);
//re-enable
$('#submit_postcode').removeAttr('disabled');