javascript 使提交输入不可点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10730568/
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
Make a submit input un-clickable
提问by David542
I have the following input:
我有以下输入:
<input type="submit" name="next" value="Next">
How would I make this item un-clickable via jQuery? (i.e., it may only be clicked aftercertain validation criteria are met) ?
我如何通过 jQuery 使这个项目不可点击?(即,只有在满足某些验证标准后才能单击它) ?
Here is a follow-up to this question: Make a submit clickable after validations are met
这是此问题的后续行动:满足验证后提交可点击
采纳答案by Tats_innit
2 Demos Demo 1 click here:orDemo 2 click here:
2 Demos Demo 1 单击此处:或Demo 2 单击此处:
so all you will need is to check if the validation is done correctly of not.
所以你只需要检查验证是否正确完成。
i.e. if (notValid)
chuck in the code below and that will disable the button. orbetter yet start with the disabled next
button.
即if (notValid)
卡在下面的代码中,这将禁用按钮。或者最好从禁用next
按钮开始。
To enable you can set the property as false, I am sure you get a good idea, else provide more code; I am happy to help you further. B-)
为了使您可以将属性设置为false,我相信您有一个好主意,否则提供更多代码;我很乐意为您提供进一步的帮助。乙-)
Hope this helps!
希望这可以帮助!
$("#submit").prop('disabled', true);?
OR
或者
$("input[type='submit']").prop('disabled', true);?
回答by Ohgodwhy
$('input[name="next"]').prop('disabled', true);
回答by Ohgodwhy
you can use:
您可以使用:
$("input[type=submit][name='next']").attr("disabled", "disabled"); // unclickable
$("input[type=submit][name='next']").removeAttr("disabled"); // clickable
回答by o.v.
Disabling an input element does not consistently prevent click events or their further propagation across browsers.
禁用输入元素并不能始终如一地阻止单击事件或其在浏览器中的进一步传播。
Regardless, it would be appropriate to simply return false
if the validation fails, so that an action (i.e. a form submit) is not performed. This will prevent form from being submitted, and since the click handler is still executed you would be able to provide details to the user about why the submission failed:
无论如何,false
如果验证失败则简单地返回是合适的,这样就不会执行操作(即表单提交)。这将阻止提交表单,并且由于单击处理程序仍在执行,您将能够向用户提供有关提交失败原因的详细信息:
$("#submit").click(function(){
var isValid = true;
/*perform validation checks, changing isValid value to false when needed*/
return isValid; //will return false if validation fails etc.
});