2016-02-02 53 views
0

我試圖只有當我的一個或兩個複選框被選中時才需要表單。我知道jQuery驗證直接支持這個,但它不工作,我不明白爲什麼。jQuery驗證所需的回調

這裏的標記:

<form id="text_opt"> 
<input id="promos" name="promos" type="checkbox" /><label for="promos">Special Promotions</label> 
<input id="products" name="products" type="checkbox" /><label for="products">New Products &amp; Services</label> 
<div class="mobile-wrap"> 
    <input id="text_phone" name="text_phone" type="text" placeholder="Mobile Phone Number" required/> 
</div> 
<p class="text-msg">*Standard Messaging fees and rates apply</p> 

這裏的功能:

function isTextChecked() { 
    return jQuery('input#promos').is(':checked') || jQuery('input#products').is(':checked') 
} 

這裏的jQuery驗證電話:

jQuery('form#text_opt').validate({ 
    rules: { 
     text_number: { 
      required: isTextChecked() 
     } 
    } 
}); 

如果我所說的isTextChecked()函數在控制檯中返回true/false,因爲我期望它,但由於某種原因,該字段是必需的,無論如何。

回答

0

您已經爲text_number字段設置了必需的規則,但是在表單中字段名稱是text_phone。它也有一個必需的屬性,它也應該被刪除。

jQuery('form#text_opt').validate({ 
    rules: { 
     text_phone: { 
      required: isTextChecked() 
     } 
    } 
}); 
+0

AHA !!工作一段時間後,我顯然失去了基本的閱讀和比較技巧。樹林和所有的森林。非常感謝! –