2013-07-04 103 views
0

我有一個在一個頁面上具有輸入字段和提交按鈕的窗體上的同一類的幾個div。我檢查每個輸入字段的jquery,如果它是空的,並且我想要在該div中的空字段時禁用該特定div中的提交按鈕。得到這個至今:jquery禁用特定提交按鈕

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。

+0

你能發佈一些你的HTML嗎? – 2013-07-04 09:56:48

+0

這是用PHP生成的,但生病嘗試重新創建它。 – Snuur

回答

2

嘗試:

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

或使用最接近:

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

謝謝!這很好。 – Snuur

2

它可以通過查找父div,相對於當前輸入,然後在div中找到按鈕來完成。

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

旁註:.prop('disabled', true)最好.attr()