简体   繁体   English

电话号码栏js

[英]phone number rails js

Is is possible to do this in rails. 可以在rails中执行此操作。 I have read about turbolinks. 我已经阅读了有关turbolinks的文章。 and have tried to adapt it for rails but was unable. 并尝试将其适应铁轨,但未能成功。

 document.getElementById('phone').addEventListener('input', function (e) { var x = e.target.value.replace(/\\D/g, '').match(/(\\d{0,3})(\\d{0,3})(\\d{0,4})/); e.target.value = !x[2] ? x[1] : '(' + x[1] + ') ' + x[2] + (x[3] ? '-' + x[3] : ''); }); 
 <input type="text" id="phone" placeholder="(555) 555-5555"/> 

this code comes from: Mask US phone number string with JavaScript 这段代码来自: 用JavaScript屏蔽美国电话号码字符串

You can include the javascript code inside the Rails App, If you are gonna use this field in all over the application you can use this script in application.js in the folder app/assets/javascripts/application.js 您可以将JavaScript代码包含在Rails应用程序内,如果要在整个应用程序中使用此字段,则可以在app / assets / javascripts / application.js文件夹中的application.js中使用此脚本。

document.getElementById('phone').addEventListener('input', function (e) {
  var x = e.target.value.replace(/\D/g, '').match(/(\d{0,3})(\d{0,3})(\d{0,4})/);
  e.target.value = !x[2] ? x[1] : '(' + x[1] + ') ' + x[2] + (x[3] ? '-' + x[3] : '');

Include this in application.js 将此包含在application.js中

If you have page specific javascripts you can create a separate js file and include that in folder app/assets/javascripts 如果您具有特定于页面的JavaScript,则可以创建一个单独的js文件,并将其包含在文件夹app / assets / javascripts中

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

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