jquery禁用特定的提交按钮

时间:2013-07-04 09:54:38

标签: jquery

我在一个页面上有几个具有相同类的div,其中包含带有输入字段和提交按钮的表单。我检查每个输入字段的jquery是否为空,并且我希望当该div中的空字段时,该特定div中的提交按钮被禁用。到目前为止得到了这个:

jQuery(document).ready(function(){
    $('.shipdiv input').each(function(){
    if( $(this).val() == ''){
    $(this).css("background-color","#ff0000");
    $(this).parent('.contact-button').attr('disabled', 'disabled');
}
});
   });

当字段为空时,它会变为红色,然后我尝试禁用其父项中的提交按钮。但那不起作用。有人知道如何做到这一点吗?

HTML看起来像这样:                                        

<div class="shipdiv">
<form name="order_002" method="post" action="">
<input type="textfield" class="ship_data" name="bedrijfsnaam"/>
<input type="textfield" class="ship_data" name="straat" />
<input type="textfield" class="ship_data" name="postcode"/>
<input type="submit" name="ship-submit" id="ship-submit" class="contact-button" value="Aanmelden">
</form>
</div>

然后有几个这样的div。

2 个答案:

答案 0 :(得分:2)

可以通过找到相对于当前输入的父div,然后在div中找到按钮来完成。

$(this).parents('.shipdiv').find('.contact-button').prop('disabled', true);

附注:.prop('disabled', true)优先于.attr()

答案 1 :(得分:2)

尝试:

 $(this).parent('div').find('.contact-button').attr('disabled', 'disabled');

或使用最接近的:

 $(this).closest('div').find('.contact-button').attr('disabled', 'disabled');