在LiveValidation中如何在Ajax回调中设置failureMessaage?

时间:2012-02-17 14:30:15

标签: jquery ajax livevalidation

枝, 我正在尝试使用jQuery LiveValidation库使用Ajax Call验证字段。 在Ajax调用中,我实际上正在检查两件事,并且无法验证它们中的任何一个将返回两个不同的消息。现在,我的Qn是我如何使用从服务器返回的消息设置failureMessage。我的JS代码如下:

var mymsg = "";
function check_avail(id, postUrl,callback)
{

    var dataVal = id+'='+$("#"+id).val();

    var isaccepted = ''
    $('#'+id).next('span').remove();
    $("#check-"+id).show();
    $.ajax({
        url: postUrl,
        cache: false,
        type: 'post',
        dataType: 'json',
        data: dataVal,
        async:  false,
        success: function(data) {
            $("#check-"+id).hide();
            if( data.success === 'true' )
            {
                isaccepted = true;
            }

            if( data.success === 'false' )
            {

                isaccepted = false;
                callback(data.message);

            }

        }
    });


    if (isaccepted === false) {
        $('.grid_16').find('input, textarea, button, select').attr('disabled','disabled');
        $('#subs_mob_no').removeAttr('disabled');


        return false;   
    } else{
        $('.grid_16').find('input, textarea, button, select').removeAttr('disabled');
        return true
    };
}
mob_no.add(Validate.Custom, { against: function(value, args) { return check_avail(subs_mob_no', '<?php echo site_url('recycledsafman/ajax_check_avilable'); ?>',function(msg){ 
            mymsg = msg;
        }); 
    },failureMessage:this.mymsg} );

任何人都可以请你建议我怎么做?!

1 个答案:

答案 0 :(得分:0)

试试这个:

var mymsg = "";
function check_avail(id, postUrl,callback)
{

    var dataVal = id+'='+$("#"+id).val();

    var isaccepted = ''
    $('#'+id).next('span').remove();
    $("#check-"+id).show();
    $.ajax({
        url: postUrl,
        cache: false,
        type: 'post',
        dataType: 'json',
        data: dataVal,
        async:  false,
        success: function(data) {
            $("#check-"+id).hide();
            if( data.success === 'true' )
            {
                isaccepted = true;
            }

            if( data.success === 'false' )
            {
                isaccepted = false;
                callback(data.message);
            }

            if (isaccepted === false) {
                $('.grid_16').find('input, textarea, button, select').attr('disabled','disabled');
                $('#subs_mob_no').removeAttr('disabled');


                return false;   
            } else {
                $('.grid_16').find('input, textarea, button, select').removeAttr('disabled');
                return true
            }
        }
    });
}
mob_no.add(Validate.Custom, { against: function(value, args) { return check_avail(subs_mob_no', '<?php echo site_url('recycledsafman/ajax_check_avilable'); ?>',function(msg){ mymsg = msg; }); },failureMessage:this.mymsg} );