【问题标题】:after validation return empty field with focus验证后返回具有焦点的空字段
【发布时间】:2016-06-09 13:38:40
【问题描述】:

我有一个对 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();
});

谢谢

【问题讨论】:

    标签: php jquery validation focus


    【解决方案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();
    }
    

    【讨论】:

      【解决方案2】:

      您只需像这样更改您的 jquery 即可。删除点击()

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

      });

      工作正常

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-19
        • 1970-01-01
        • 2018-06-25
        • 1970-01-01
        • 2015-02-09
        • 2021-01-23
        • 1970-01-01
        • 2014-06-27
        相关资源
        最近更新 更多