【问题标题】:How do we set mask on a dynamically inserted input?我们如何在动态插入的输入上设置掩码?
【发布时间】:2014-02-06 16:45:34
【问题描述】:

由于Angular-UI-Mask is acting oddly,我在我的一些输入中使用jquery-inputmask,但是当输入动态插入ny Angular时,它没有掩码:

<li ng-repeat="item in items">
    <input type="text" name="birth_date" class="span2 format_date" ng-model="birth_date" placeholder="Data de Nascimento" required />
</li>

这是相关脚本

<script type="text/javascript">
    jQuery(document).ready(function(){
        $(".format_date").inputmask("99/99/9999");
    });
</script>

我可以做些什么来强制它将掩码设置为新的输入?

【问题讨论】:

    标签: javascript jquery angularjs jquery-inputmask


    【解决方案1】:

    jQuery.inputMask 之类的 jQuery 插件通过(如您的代码所示)在文档“就绪”时将行为附加到 DOM 元素。这将运行一次,并且永远不会再次运行,因此对于动态添加的内容,这种方法不起作用。

    相反,您需要在相应的 DOM 更改时运行的东西。因此,每当添加“项目”列表中的“项目”时,都会添加该元素并针对该元素运行相应的 jQuery 函数。您需要为此使用 AngularJS,并且可以编写自己的指令,但幸运的是,有人已经为您编写了代码:jQuery Passthrough plugin 作为Angular UI's UI.Utils 的一部分。

    这是working Plunkr

    您需要在顶部包含脚本,就像这样 (I downloaded it from GitHub):

    <script src="ui-utils.jq.js"></script>
    

    将模块加载到AngularJS中,例如:

    var app = angular.module('myApp', ['ui.jq']); 
    

    然后在您的 HTML 标记中使用该指令:

    <input type="text" ui-jq="inputmask" ui-options="'99/99/9999', { 'placeholder': 'dd/mm/yyyy' }" /> 
    

    【讨论】:

    • 效果很好。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    相关资源
    最近更新 更多