Javascript 如何使用 jQuery 禁用 textarea + 提交按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4465849/
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 do I use jQuery to disable a textarea + Submit button?
提问by TIMEX
After a user submits a comment, I want the textarea and summit button to be "disabled" and somewhat visually disabled.
用户提交评论后,我希望 textarea 和 Summit 按钮被“禁用”并且在视觉上有些禁用。
Like Youtube.
就像优酷一样。
How can I do that with Jquery using the simplest plugin and/or method?
如何使用最简单的插件和/或方法使用 Jquery 做到这一点?
回答by Jonathon Bolster
Simply set the disabled
attribute on your input elements when the button is clicked:
disabled
单击按钮时,只需在输入元素上设置属性:
$("#mybutton").click(function(){
$("#mytext,#mybutton").attr("disabled","disabled");
});
Example: http://jsfiddle.net/jonathon/JcXjG/
回答by Adam Batkin
$(document).ready(function() {
$('#idOfbutton').click(function() {
$('#idOfTextarea').attr("disabled", "disabled");
$('#idOfbutton').attr("disabled", "disabled");
});
});
This basically says: When the document is "ready", attach an event handler to the button's (HTML ID "idOfButton") click event which will set the disabled
attribute of the textarea (HTML ID "idOfTextarea") and the button.
这基本上是说:当文档“准备好”时,将事件处理程序附加到按钮的(HTML ID“idOfButton”)单击事件,该事件将设置disabled
文本区域(HTML ID“idOfTextarea”)和按钮的属性。
回答by Trevor
$('form').submit(function(){
return false;
});
回答by Enrique
jQuery(document).ready(function() {
$('form').submit(function(){
$('input[type=submit]', this).attr('disabled', 'disabled');
});
});
回答by Fatih Acet
$('#btn').click(function(){ $(this, '#textarea').attr('disabled', 'disabled'); })
回答by driangle
So first handle the event where the user submits the comment and then disable the textarea and submit button. (assuming your submit button can be selected with "input#submit-comment" and your textarea can be selected with "textarea". The addClass part is optional but can be used for you to style those elements differently if they happen to be disabled.
所以首先处理用户提交评论的事件,然后禁用textarea和提交按钮。(假设您的提交按钮可以使用“input#submit-comment”选择,而您的 textarea 可以使用“textarea”选择。addClass 部分是可选的,但如果它们碰巧被禁用,您可以使用它们以不同的方式设置样式。
$("input#submit-comment").click(function(){
$("textarea").attr("disabled", "disabled").addClass("disabled");
$(this).attr("disabled", "disabled").addClass("disabled");
// ... Actually submit comment here, assuming you're using ajax
return false;
}