[英]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?有人可以帮助我吗?
add require添加要求
require("jquery-mask-plugin")
add line at the buttom在底部添加线
$.jMaskGlobals.watchDataMask = true;
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' %>
in app/javascript/packs/application.js
在
app/javascript/packs/application.js
$(document).on('turbolinks:load', function () { var im = new Inputmask('999.999.999-99'); var selector = $('#person_document'); im.mask(selector); });
yarn add inputmask
in app/javascript/packs/application.js
在
app/javascript/packs/application.js
require('inputmask');
in assets/javascript/application.js
在
assets/javascript/application.js
//= require jquery //= require jquery_ujs //= require jquery-mask-plugin //= require_tree . //= require jquery.inputmask.bundle.min $.jMaskGlobals.watchDataMask = true;
in app/javascript/packs/application.js
在
app/javascript/packs/application.js
require('inputmask');
This solved the problem for me.这为我解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.