簡體   English   中英

jquery.validate 因“a 為空”而失敗

[英]jquery.validate failing with “a is null”

我有一個我一直在構建的 ASP.NET MVC 應用程序,我正在嘗試驗證單個字段。 因此,我決定將 jQuery.validate 引入我的應用程序,因此如果我將來需要進行更多驗證,我將包含該插件。

做了一些測試,我補充說:

jQuery("#NextFollowUpDateTimePicker").rules("add", {
  minlength: 2
});

我的輸入 ID 是“NextFollowUpDateTimePicker”,我什至還沒有嘗試驗證,但我只是想添加規則。 當我使用 Firebug 檢查控制台時,出現以下錯誤:

a is null

這是從我的 jQuery 文件 (1.5.1) 中拋出的。 有沒有其他人遇到過這個? 我包含了各種其他 JS 文件,所以我猜這是某種沖突,但我不確定如何調試它。

提前致謝! -馬特

確保您已調用$("#myForm").validate(); 在添加任何規則之前。 此外,如果您要添加動態輸入以進行驗證,請確保在添加規則之前將它們添加到 DOM。

不確定下面的代碼是否會幫助某人,因為當我在單擊提交按鈕時調用 JQuery 驗證器以驗證字段並執行 AJAX 調用時,我確實遇到了不適當的行為。 在我添加以下語句之前,多次提交時驗證未按預期工作。 看起來 DOM 需要在表單提交時重置,以避免任何瀏覽器刷新以使驗證按預期工作。

$.data($(id)[0], 'validator', null);

顯示輸入類型提交的JSP文件

<input type="submit" id="label.login.button" name="label.login.button"
       value="" class="btn btn-primary btn-xs btn-block">

JavaScript執行驗證和處理表單提交

 /**
 * 
 * @author dinesh.lomte
 */

/**
 * 
 */
$(document).ready(function() {
    reset('#loginForm');
    // Validating the login form on submit  
    validateLoginForm();
});

/**
 * 
 * @param id
 * @returns
 */
function reset(id) {    
    $.data($(id)[0], 'validator', null);
}

/**
 * 
 * @returns {undefined}
 */
function validateLoginForm() {
    // Validating the login form
    $('#loginForm').validate({
        rules: {
            userId:     {
                required:   true,
                userId:     true
            },
            password:   'required'
        },
        messages: {
            userId: {
                required: function() {
                    // Message: Please enter User ID.
                    return $.i18n('business.message.00001');
                },
                userId: function() {
                    // Message: Invalid character(s) found. Please enter valid characters.
                    return $.i18n('business.message.00003');
                }
            },
            password: {
                required: function() {
                    // Message: Please enter Password.
                    return $.i18n('business.message.00002');
                }
            }
        },
        submitHandler: function(form) {
            // Processing the login request
            processLoginRequest();
        }
    });
    return false;
}

/**
 * 
 * @returns
 */
function processLoginRequest() {
    // Building the json instance to process login request
    var formData = {'userId':$('#userId').val(),
                    'password':$('#password').val(),
                    'language':$('#language').val()
                    };
    $.ajax({
        type        :'POST',
        contentType :'application/json',
        url         :getContextPath() + '/login',
        data        :JSON.stringify(formData),
        dataType    :'json',
        beforeSend  :function() {
            $('#pleaseWaitModal').modal();
        },
        complete    :function() {
            $('#pleaseWaitModal').modal('hide'); 
        },
        success     :function(data) {
            // Validating ui message(s) for display
            if(hasUiMessages(JSON.stringify(data))) {
                return;
            }
            createHomeControllerForm(data);
            toggleComponents(true);
        },
        error       :function(exception) {
            // Message: Failed to process your request due to system 
            //          error. Please contact your system administrator.
            showI18nMessage('error', 'business.message.00005');
            toggleComponents(false);
        }
    });
}

暫無
暫無

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

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