JQuery Masked Input插件不起作用 [英] JQuery Masked Input plugin doesn't work

查看:88
本文介绍了JQuery Masked Input插件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的Web项目中添加了一个JQuery Masked Input插件,但它根本不起作用。

I've added a JQuery Masked Input plugin to my Web project but it's not working at all.

该插件可以在这里找到: http://digitalbush.com/projects/masked-input-plugin

The plugin can be found here: http://digitalbush.com/projects/masked-input-plugin

我在我的JSP中包含了JQuery libray和Masked Input插件,并为我的<$调用了 mask 函数c $ c> html< input> 元素:

I've included JQuery libray and the Masked Input plugin to my JSP, and called the mask function for my html <input> element:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <!-- JS --->
    <script src="js/jquery-1.11.0.js"></script>
    <script src="js/masked-input-jquery-1.3.1.js"></script>

    <title>Test</title>
</head>
<body>

    <script type="text/javascript">
       $("#name").mask("99/99/9999");

    </script>

    <form id="" method="" action="">
    <div>
       <label for="name">
          Name<b style="color: red">*</b>
       </label>
       <input name="studentName" maxlength="255" autofocus="autofocus" type="text" id="name"/> 
......

当我访问我的JSP时,甚至在输入任何内容之前在文本字段中,以下内容显示在Chrome控制台上:

When I access my JSP, even before typing anything on the text field, the following appears on Chrome console:

未捕获的ReferenceError:未定义iMask

你能帮帮我吗?代码有什么问题吗?

Can you help me? Is there anything wrong with the code?

推荐答案

这可能会或可能不会解决您当前的问题,但您对.mask的调用将会不起作用,因为它在页面的其余部分(输入字段所在的位置)之前运行。

This may or may not fix your current problem, but your call to .mask will not work because it runs before the rest of the page (where your input fields are) is parsed.

你需要在jQuery文档就绪函数中包装调用:

You need to wrap the call in the jQuery document ready function:

$('document').ready(function() {
    $("#name").mask("99/99/9999");
});

这告诉脚本要等到页面加载到足以让浏览器知道输入字段在文档中。

This tells the script to wait until the page is loaded enough for the browser to know about the input fields in the document.

作为附加注释的最佳实践说,将所有脚本标记(有一些例外)放在结束标记之前。
否则你应该将脚本标签移动到其他标签的头部。

As an additional comment best practices say to put all script tags (with some exceptions) just before the closing body tag. Otherwise you should move the script tag into the head with the rest of the tags.

这篇关于JQuery Masked Input插件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆