验证后 return 具有焦点的空字段

after validation return empty field with focus

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

验证:

    $('#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();
});

谢谢

你只需像这样改变你的jquery。删除点击()

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

});

工作正常

您只需添加 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();
}