简体   繁体   English

JQuery 屏蔽输入在 Rails 6 中不起作用

[英]JQuery Masked Input Does Not Working in Rails 6

I am trying to use the JQuery Masked Input in my form on Rails 6. But it seems to me that the application.js in assets is not working, i am not sure.我正在尝试在 Rails 6 上的表单中使用 JQuery 屏蔽输入。但在我看来,资产中的application.js不起作用,我不确定。 I installed the plugin through yarn with the command: yarn add jquery-mask-plugin .我使用以下命令通过 yarn 安装了插件: yarn add jquery-mask-plugin It was included in package.json as a dependency.它作为依赖项包含在package.json中。

To setup, I went to app/assets/javascripts/application.js .为了设置,我去了app/assets/javascripts/application.js This is the file:这是文件:

//= require jquery
//= require jquery_ujs
//= require jquery-mask-plugin
//= require_tree .

$.jMaskGlobals.watchDataMask = true;

In app/javascript/packs/application.js I tried to import the JQuery and the plugin ass well:app/javascript/packs/application.js 中,我也尝试导入 JQuery 和插件:

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")
import 'inputmask';

Now in my form, this is how I tried to use the plugin:现在以我的形式,这就是我尝试使用插件的方式:

<%= form.text_field :cpf, class:'form-control', 'data-mask': '999.999.999-99' %>

As I said before, I am not sure, but I think the problem is the application.js file in assets, cause I wrote a console.log("test") command just to see if it is executing and nothing appeared at the web console when the server was running.正如我之前所说,我不确定,但我认为问题是资产中的application.js文件,因为我写了一个console.log("test")命令只是为了查看它是否正在执行,而网络上什么也没有出现服务器运行时的控制台。

Can someone would help me?有人可以帮助我吗?

  1. yarn add "jquery-mask-plugin"纱线添加“jquery-mask-plugin”
  2. in app/javascript/packs/application.js在 app/javascript/packs/application.js 中

add require添加要求

require("jquery-mask-plugin")
  1. in app/javascript/packs/application.js在 app/javascript/packs/application.js 中

add line at the buttom在底部添加线

$.jMaskGlobals.watchDataMask = true;
  1. in .html.erb在 .html.erb 中

    = d.text_field :phone_number, autocomplete: "phone Number", class: "form-control", placeholder: "03xx-xxxxxxx" , 'data-mask': '0000-0000000' = d.text_field :phone_number, autocomplete: "phone Number", class: "form-control", placeholder: "03xx-xxxxxxx", 'data-mask': '0000-0000000'

or in html.erb在 html.erb

<%= d.text_field :phone_number, autocomplete: "phone Number", class: "form-control", placeholder: "03xx-xxxxxxx" , 'data-mask': '0000-0000000' %>
  1. in app/javascript/packs/application.jsapp/javascript/packs/application.js

     $(document).on('turbolinks:load', function () { var im = new Inputmask('999.999.999-99'); var selector = $('#person_document'); im.mask(selector); });
  2. yarn add inputmask

  3. in app/javascript/packs/application.jsapp/javascript/packs/application.js

     require('inputmask');
  4. in assets/javascript/application.jsassets/javascript/application.js

     //= require jquery //= require jquery_ujs //= require jquery-mask-plugin //= require_tree . //= require jquery.inputmask.bundle.min $.jMaskGlobals.watchDataMask = true;
  5. in app/javascript/packs/application.jsapp/javascript/packs/application.js

     require('inputmask');

This solved the problem for me.这为我解决了这个问题。

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

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