简体   繁体   English

使用js填充具有信用卡格式的输入字段

[英]Filling Input fields having credit card formatting using js

I was wondering if there is a way to fill the input fields having type text and have inbuilt js event to evaluate what is being entered means, when a user types in any number it evaluates and checks the type of card and also put automatic spaces in between.我想知道是否有一种方法可以填充具有文本类型的输入字段并具有内置的 js 事件来评估正在输入的内容,当用户输入任何数字时,它会评估并检查卡的类型并在其中放置自动空格之间。 But while setting the value with javascript ie element.value = 'xxxxxxxx';但是在使用 javascript 设置值时,即element.value = 'xxxxxxxx'; the formatting doesn't happen and the site evaluates the card number invalid.格式化不会发生,并且该站点评估卡号无效。 so how to programmatically achieve this.那么如何以编程方式实现这一点。 I am working on an extension which could auto fill card details.我正在开发一个可以自动填写卡片详细信息的扩展程序。

I have tried using element.dispatchEvent(Keyboardevent) but it won't work.我试过使用element.dispatchEvent(Keyboardevent)但它不起作用。

the website on which i am trying is made on top of angular.我正在尝试的网站是在 angular 之上制作的。

I have found a workaround.我找到了一种解决方法。 It was something like changing the value and then dispatching an event.这就像更改值然后调度事件。 Since I was working with angular I needed to dispatch input event.由于我正在使用 angular 我需要调度输入事件。 Related codes are:相关代码为:

element.value = 'anything';
//dispatching input event after setting value ( tested for angular) 
element.dispatchEvent(new Event('input')) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM