首页 > 解决方案 > Javascript keyup enter 无法正常工作

问题描述

我有检查我国国家代码的通用算法。每个代码都有 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,按钮:真,自动:真,


0011433875:按钮:假,自动:真,


0011433874:按钮:假,自动:真,


所有按钮都以“正确”回答,但只有第一个是正确的。

标签: javascriptvue.js

解决方案


推荐阅读