3

我正在尝试在我的数据表编辑器字段之一(#DTE_Field_phone)上使用Masked Input插件。由于某种原因,此代码未应用掩码:

$('#DTE_Field_phone').mask('(999) 999-9999');

我在表和编辑器初始化后调用掩码,但仍然没有。关于我可能做错了什么的任何想法?

HTML:

<div class="DTE_Field_Input" data-dte-e="input">
    <input id="DTE_Field_phone"></input>
    <div class="DTE_Field_Error" data-dte-e="msg-error" style="display: none;"></div>
    <div class="DTE_Field_Message" data-dte-e="msg-message"></div>
    <div class="DTE_Field_Info" data-dte-e="msg-info"></div>
</div>

jQuery:

jQuery(function ($) {
    $( document ).ready(function (e) {
        var editor = new $.fn.dataTable.Editor({
            "ajaxUrl": "../wp-content/plugins/contacts/php/table.wp_contacts.php",
            "domTable": "#form_results7",
            "fields": [
                { "label": "Contact",
                  "name": "contact",
                  "type": "text" },
                { "label": "Company",
                  "name": "company",
                  "type": "text" },
                { "label": "Email",
                  "name": "email",
                  "type": "text" },
                { "label": "Phone",
                  "name": "phone",
                  "type": "text" },
                { "label": "Fax",
                  "name": "fax",
                  "type": "text" },
                { "label": "Address",
                  "name": "address",
                  "type": "text" },
                { "label": "Tax ID",
                  "name": "tax_id",
                  "type": "text" }
            ]
        });
        $('#add_items').on('click', function (e) {
            e.preventDefault();
            editor.create(
                'Add Contact',
                {
                    "label": "Add",
                    "fn": function () {
                        editor.submit()
                    }
                }
            );
        });
        $('#form_results7').on('click', 'a.editor_edit', function (e) {
            e.preventDefault();
            editor.edit(
                $(this).parents('tr')[0],
                'Edit Contact',
                { "label": "Update", "fn": function () { editor.submit() } }
            );
        });
        $('#form_results7').on('click', 'a.editor_remove', function (e) {
            e.preventDefault();
            editor.message( "Are you sure you want to remove this row?" );
            editor.remove( $(this).parents('tr')[0], 'Delete row', {
                "label": "Confirm",
                "fn": function () { this.submit(); }
            });
        });
        var oTable = $('#form_results7').dataTable({
            "bAutoWidth": false,
            "bJQueryUI": true,
            "sAjaxSource": "../wp-content/plugins/contacts/php/table.wp_contacts.php",
            "sDom": "<'H'lfr>t<'F'ip>",
            "aoColumns": [
                { "mData": "contact",
                  "sWidth": "14%" },
                { "mData": "company",
                  "sWidth": "14%" },
                { "mData": "email",
                  "sWidth": "17%" },
                { "mData": "phone",
                  "sWidth": "11%" },
                { "mData": "fax",
                  "sWidth": "11%" },
                { "mData": "address",
                  "sWidth": "17%" },
                { "mData": "tax_id",
                  "sWidth": "8%" },
                { "bSortable": false,
                  "mData": null,
                  "sClass": "center",
                  "sDefaultContent": '<a href="" class="editor_edit">Edit</a> | <a href="" class="editor_remove">Delete</a>',
                  "sWidth": "8%" }
            ],
            "sPaginationType": "full_numbers"
        });
        $('#DTE_Field_phone').mask("(999) 999-9999");
        try {$("#form_results7_length select").msDropDown();} catch(e) {alert(e.message);}
        $('#refresh_items').click(function() {
            location.reload();
        });
        $("#reset").click(function() {
            $("#form_results7_filter input").val("");
            oTable.fnFilter("");
            oTable.fnSort([[ 0, "asc" ]]);
        });
    });
});
4

2 回答 2

2

我没有数据表的经验,但从您的代码中我认为您正在使用弹出编辑器?如果是这样,您可能需要在编辑器打开后触发的事件中应用掩码,如下所示:

editor.on('onOpen', function () {
    $('#DTE_Field_phone').mask('(999) 999-9999');
});
于 2013-08-23T15:31:34.837 回答
0

您在 a 中声明一个$( document ).ready(function (e) {函数Jquery(function($),这是错误的......

jQuery(function ($) {
    $( document ).ready(function (e) {
    ...
    }
}

您不能同时使用这两个函数,因为它们与同一个事件挂钩。

请参阅文档中的此处。

解决方案 1

jQuery(function ($) {
...
}

解决方案 2

$( document ).ready(function (e) {
...
}
于 2013-08-23T15:24:39.490 回答