开发者

Selecting next submit after actual textarea and disable it

I have various instances of a form in a page. I want to desactivate the submit button of that ones开发者_JAVA百科 with the default text in the textarea. My actual code is:

// This works
$(".proposal-write-comment-submit").each(function(){
    var element = $(this).closest(".proposal-write-comment-textarea");

    var default_text = element.attr("data-default");
    var value_text   = element.val();

    if(default_text == value_text) {
        $(this).attr("disabled","disabled");    
    }
});

// This no
$(".proposal-write-comment-textarea").keypress(function(e){
    var default_text = $(this).attr("data-default");
    var value_text   = $(this).val();

    if(default_text == value_text) {
        $(this).closest(".proposal-write-comment-submit").attr("disabled","disabled");  
    } else {
        $(this).closest(".proposal-write-comment-submit").removeAttr("disabled");
    }
});

My HTML code:

<form class="proposal-write-comment" method="post" action="">
    <textarea class="proposal-write-comment-textarea" data-default="Write a comment...">Write a comment...</textarea>
    <input class="proposal-write-comment-submit" type="submit" value="Save" />
</form>

The second part of the javascript doesn't works. How I can select right the submit buttons "next to" the textarea?

Thank you in advance!


You could try the 'siblings' selector -

if(default_text == value_text) {
        $(this).siblings(".proposal-write-comment-submit").attr("disabled","disabled");  
    } else {
        $(this).siblings(".proposal-write-comment-submit").removeAttr("disabled");
    }


closest looks up the DOM tree, at parents and ancestors, rather than at siblings. As the input element is a sibling of the textarea element, you need to look at siblings. To do that, you could use next, (assuming the input always directly follows the textarea... if not, you could use nextAll or siblings with the appropriate selector)

$(this).next().removeAttr("disabled");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜