验证后返回带焦点的空字段

时间:2016-02-26 11:01:47

标签: php jquery validation focus

我有一个对pincode进行验证的应用程序。 验证错误时会显示错误消息,但随后它会将密码保留在字段中并失去焦点。 当你第一次进入模态输入密码时,它会聚焦好并打开模态键盘。当你必须再次填写密码时,我想要一样的。 下面的代码用于聚焦和验证:

验证:

    $('#suspend_sale').click(function() {
        var custID  = $("#spos_customer").val(),
            pin     = $("#reference_note").val();
        $.ajax({
            url: base_url + 'customers/validatePin',
        data: {
            custID: custID,
            pin: pin
        },
        type: "get",
        dataType: 'json',
        success: function(response) {
            if(response.valid) {
                $('#hold_ref').val($('#reference_note').val());
                $('#total_items').val(an - 1);
                $('#total_quantity').val(count - 1);
                $('#submit').click();
            } else {
                $("#ref_error_msg").show(function(){
                    setTimeout(function(){
                        $("#ref_error_msg").hide('slow');
                    }, 1000);
                });
            }
        },
        error: function() {
            bootbox.alert(lang.customer_request_failed);
            return false;
        }
    });
    return false;
}); 

首先关注输入字段:

    $('#susModal').on('shown.bs.modal', function () {
    // clear input field, set focus and click to make keyboard appear
    $('#reference_note').val('').focus().click();
});

感谢

2 个答案:

答案 0 :(得分:1)

你只需改变你的jquery就好了。删除click()

   $('#susModal').on('shown.bs.modal', function () {
// clear input field, set focus and click to make keyboard appear
$('#reference_note').val('');
$('#reference_note').focus();

});

工作正常

答案 1 :(得分:1)

您只需要添加1个额外的行:

else {
     $("#ref_error_msg").show(function(){
         setTimeout(function(){
             $("#ref_error_msg").hide('slow');
         }, 1000);
     });
     //after showing the error message you need to clear the input & focus upon it
     $('#reference_note').val('').focus();
}