JavaScript键盘输入无法正常工作

时间:2019-07-31 08:08:41

标签: javascript vue.js

我有检查国家代码的通用算法。每个代码都有10位数字:

function Nationalcode(code)
{

  var L=code.length;

  if(L<8 || parseInt(code,10)==0) return false;
  code=('0000'+code).substr(L+4-10);
  if(parseInt(code.substr(3,6),10)==0) return false;
  var c=parseInt(code.substr(9,1),10);
  var s=0;
  for(var i=0;i<9;i++)
    s+=parseInt(code.substr(i,1),10)*(10-i);
  s=s%11;
  return (s<2 && c==s) || (s>=2 && c==(11-s));
return true;
}

当我添加方法并使用@ click =“ check”运行该方法时,效果很好:

check: function(){
  var c=document.getElementById("CodeM").value;
  if( checkCodeMeli(c))
    alert("true");
  else
    alert("fasle");
}

但是我想在用户输入10位数字时自动检查:

<input type="text" v-model="nationalcode" id="CodeM" v-on:keyup.enter="queryForKeywords" v-on:keydown="queryForKeywords">

    queryForKeywords: function() {
      this.$nextTick(() => {
        var c=document.getElementById("CodeM").value;
        this.isActive = 'false';
        if (this.nationalcode.length == 9 ) {
            if( checkCodeMeli(c)){
                alert("true");
            }
            else{
                alert("false");
            }
        }
        else{

        }
      });
    },

此方法会产生不正确的结果。例如,当我输入:

0011433876, 按钮:是的, auto:true,


0011433875: 按钮:假, auto:true,


0011433874: 按钮:假, auto:true,


所有按钮的答案均为“正确”,但只有第一个正确。

0 个答案:

没有答案
相关问题