我有一个信用卡输入字段,我想在用户输入卡号的前 4 位数字后运行一个函数,以指示卡类型。
我希望该函数仅运行一次,然后停止。
我尝试使用计时器,但我不喜欢结果,因为表单每隔几秒就会重新加载。我也不想等到用户停止输入整个数字。
最佳答案
您可以简单地通过 onkeyup 进行检查事件,像这样:
var testIt = function testIt(num) {
if (num.length === 4) {
alert("more than 4 characters have been entered");
}
}
Card number: <input type="number" onkeyup="testIt(this.value);" />
只有输入第四个数字才会执行。
关于javascript - 用户在输入字段中输入前 4 位数字后运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31608852/