簡體   English   中英

第一次單擊按鈕時如何驗證字段?

[英]how to validate the fields when I click on a button at the first time?

當我第二次單擊按鈕時,將進行領域教育和道德教育的驗證。

我希望在第一次單擊按鈕時進行驗證。

我有以下代碼:

$('body').on('change', '#education', function(){
    if( !$("#education").val() ) {
        $(this).closest('.form-group').addClass('has-error');   
    }else{
        $(this).closest('.form-group').removeClass('has-error');
    }
});

$('body').on('change', '#moraleducation', function(){
    if( !$("#moraleducation").val() ) { 
        $(this).closest('.form-group').addClass('has-error');
    }else{
        $(this).closest('.form-group').removeClass('has-error');
    }
});

//education
$('body').on('click', '#update', function(){
    if($("body #education").hasClass('parsley-error')){
        $("body #education").closest('.form-group').addClass('has-error');
    }  
});

//moraleducation
$('body').on('click', '#update', function(){
    if($("#moraleducation").hasClass('parsley-error')){
        $("#moraleducation").closest('.form-group').addClass('has-error');
    }   
});

單擊控制台控制台,單擊時查看是否有錯誤。 您其余的代碼是什么樣的? 可能是您放置函數的順序。

我會加一個瘋狂的猜測,說你可能想要這個

$(document).on('click', '#update', function() {
    var edu_err = $('#education').hasClass('parsley-error');
    var mor_err = $('#moraleducation').hasClass('parsley-error');

    $('#education').closest('.form-group').toggleClass('has-error', !edu_err);
    $('#moraleducation').closest('.form-group').toggleClass('has-error', !mor_err);
});

根據后代錯誤類等在錯誤類之間進行切換的位置

暫無
暫無

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

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