开发者

How do I use jQuery to disable a textarea + Submit button?

After a user submits a comment, I want the textarea and summit button to be 开发者_如何学Python"disabled" and somewhat visually disabled.

Like Youtube.

How can I do that with Jquery using the simplest plugin and/or method?


Simply set the disabled attribute on your input elements when the button is clicked:

$("#mybutton").click(function(){
   $("#mytext,#mybutton").attr("disabled","disabled"); 
});

Example: http://jsfiddle.net/jonathon/JcXjG/


$(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.


 $('form').submit(function(){
        return false;
    });


    jQuery(document).ready(function() {
    $('form').submit(function(){
        $('input[type=submit]', this).attr('disabled', 'disabled');
    });
   });


$('#btn').click(function(){
    $(this, '#textarea').attr('disabled', 'disabled');
})


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.

$("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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜