我在页面中有各种表单实例。我想用textarea中的默认文本取消激活那些提交按钮。我的实际代码是:
// 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");
}
});
我的HTML代码:
<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>
javascript的第二部分不起作用。我如何选择“textarea”旁边的提交按钮?
提前谢谢!
答案 0 :(得分:1)
您可以尝试“siblings”选择器 -
if(default_text == value_text) {
$(this).siblings(".proposal-write-comment-submit").attr("disabled","disabled");
} else {
$(this).siblings(".proposal-write-comment-submit").removeAttr("disabled");
}
答案 1 :(得分:0)