当前位置:嗨网首页>书籍在线阅读

10-解决方案

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

11.2.2 解决方案

Masked input插件(http://jquery-cookbook.com/go/plugin-masked-input)是一个能够改进反馈的jQuery插件。它适用于一个或者多个输入字段,限制输入的字符,同时自动插入定界符。

在下面的例子中,把电话号码输入字段限制为固定格式:

<!DOCTYPE html>
<html>
<head>
   <script src="assets/jquery-latest.js"></script>
   <script src="assets/jquery.maskedinput.js"></script>
   <script>
   jQuery(document).ready(function($) {
     $("#phone").mask("(999) 999-9999");
   });
   </script>
</head>
<body>
   <form>
     <label for="phone">Phone</label>
     <input type="text" name="phone" id="phone" />
   </form>
</body>
</html>

除了jQuery以外,还包含插件文件。在文档就绪事件的回调函数中,选择ID为 phone 的输入字段,调用 mask 方法。唯一的参数指定了限定的格式,表示该字段格式为美国电话号码。