簡體   English   中英

jQuery驗證必需的回調

[英]jQuery Validation required callback

我試圖僅在選中我的一個或兩個復選框時才需要填寫表格。 我知道jQuery validate直接支持此功能,但是它不起作用,我不知道為什么。

這是標記:

<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 Validation調用:

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

如果我在控制台中調用isTextChecked()函數,則它會按我期望的那樣返回true / false,但是由於某種原因,無論如何都需要該字段。

您已經為text_number字段設置了必需的規則,但是在您的表單中,字段名稱為text_phone。 它還具有必填屬性,也應將其刪除。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM