Javascript代码无法在chrome中使用

时间:2014-01-06 16:21:31

标签: javascript google-chrome jquery

我正在将此JavaScript代码与JQuery Library一起使用,现在这不适用于 Google Chrome ,适用于 Apple Safari FireFox (经测试)

$(document).on('blur', '#UserNameInput', function(e) {
    //alert ('this')
    if ($(this).val().length < 6 ) {
        $('#UnameCheck').html('That user anme is too short... please try again with something more than 6 Charecters')
        } else {
    $(this).css('background-image', 'url(../images/IMG_8485.GIF)');
    $('#UnameCheck').html('Please wait while checking your User Name').css('color', '#F3F').show('fast');   
    var Uname = ($('#UserNameInput').val())
    var data = {     // create object
            l : Uname
        }
    $.ajax({
    type:"POST",
    data: data,
    complete: function(){
        $('#UserNameInput').css('background-image', '')
        },
    url:"../php/UserNameCheck.php"
    }).done(function(feedback){
    $('#UnameCheck').html(feedback)
    });
    }
});

2 个答案:

答案 0 :(得分:2)

在您的情况下,要符合Chrome要求,您需要添加mouseup事件,或/和mouseleave

$(document).on('blur mouseup mouseleave', '#UserNameInput', function (e) {
    //alert ('this')
    if ($(this).val().length < 6) {
        $('#UnameCheck').html('That user anme is too short... please try again with something more than 6 Charecters');
    } else {
        $(this).css('background-image', 'url(../images/IMG_8485.GIF)');
        $('#UnameCheck').html('Please wait while checking your User Name').css('color', '#F3F').show('fast');
        var Uname = ($('#UserNameInput').val());
        var data = { // create object
            l: Uname
        };
        $.ajax({
            type: "POST",
            data: data,
            complete: function () {
                $('#UserNameInput').css('background-image', '');
            },
            url: "../php/UserNameCheck.php"
        }).done(function (feedback) {
            $('#UnameCheck').html(feedback);
        });
    }
});

答案 1 :(得分:0)

对于FF Mac,Saf Win,Chrome Win和Chrome Mac,blur事件不完整。

these event compatibility tables中所列。

使用其他完整的事件监听器(例如mouseleavemouseup)可以解决问题。