为什么我的JS中的focus()方法不能作为onfocus事件处理?

时间:2018-11-05 10:47:22

标签: javascript focus onfocus

Dom:

<input id="bankCardInput" type="text"  onfocus="console.log('inputBox is focused');" v-model="AcctNo" v-change="CardBinQry2()" v-blur="CardBinQry()"  maxlength="19" style="width: 70%;">

JS:

$scope.CardBinQry2 = function () {
    if (!$scope.AcctNo) {
        return;
    }
    var params = {
        "AcctNo": $scope.AcctNo
    };
    if ($scope.AcctNo.length < 16) {
        $scope.BankExist = false;
        $scope.BankName = '';
        return;
    }
    $remote.post("BankAmericardQuery.do", params, function (data) {
        if (data.resultMap.BankName.indexOf('银行') > -1) {
            $scope.BankName = data.resultMap.BankName;
            $scope.BankExist = true;
        }
        setTimeout(function () {
            var input = document.getElementById('bankCardInput');
            input.focus();
        },1000)
    },function (error) {
        if (error.jsonError[0]._exceptionMessageCode == "not.supported.by.the.bank.card")
            $scope.BankName = "卡不支持";
    })
};

每次,CardBinQry2()方法都运行成功,我想查看“ inputBox已聚焦”日志,但是没有。有人告诉我为什么吗?

1 个答案:

答案 0 :(得分:0)

您可以使其功能

  setTimeout(function () {
    callback()
  },1000)
  function callback() {
    var input = document.getElementById('bankCardInput');
    input.focus();
  }