表单输入onfocus问题
我想在我的注册表中使用 jQuery Masked 输入插件 (http://digitalbush.com/projects/masked-input-plugin/)。我的表单看起来像这样
......
<input class="part1" type="text" name="lname" value="Last Name" onfocus="if(this.value=='Last name') {this.style.color='#aea592';this.style.fontStyle='normal'; this.value=''};" onblur="if(this.value=='') {this.style.color='#aea592'; this.style.fontStyle='italic'; this.value='Last name'}" />
<input class="part2" type="text" id="dob" name="dob" value="Date of Birth" onfocus="if(this.value=='Date of Birth') {this.style.color='#aea592';this.style.fontStyle='normal'; this.value=''};" onblur="if(this.value=='') {this.style.color='#aea592'; this.style.fontStyle='italic'; this.value='Date of Birth'}" />
当
文件末尾
<script src="core/code/js/mask.js"></script>
<script type="text/javascript">
jQuery(function($) {
$('#dob').mask('99.99.9999', {placeholder:' '});
});
</script>
您打开页面时,姓氏字段显示单词“姓氏”,但出生日期不显示任何内容。 http://prntscr.com/2miaa 如何仅针对 onfocus 事件激活遮罩插件?
I want to use jQuery Masked Input Plugin (http://digitalbush.com/projects/masked-input-plugin/) in my registration form. My form looks like that
...
<input class="part1" type="text" name="lname" value="Last Name" onfocus="if(this.value=='Last name') {this.style.color='#aea592';this.style.fontStyle='normal'; this.value=''};" onblur="if(this.value=='') {this.style.color='#aea592'; this.style.fontStyle='italic'; this.value='Last name'}" />
<input class="part2" type="text" id="dob" name="dob" value="Date of Birth" onfocus="if(this.value=='Date of Birth') {this.style.color='#aea592';this.style.fontStyle='normal'; this.value=''};" onblur="if(this.value=='') {this.style.color='#aea592'; this.style.fontStyle='italic'; this.value='Date of Birth'}" />
...
a the end of the file
<script src="core/code/js/mask.js"></script>
<script type="text/javascript">
jQuery(function($) {
$('#dob').mask('99.99.9999', {placeholder:' '});
});
</script>
When you open the page, Last Name field shows word "Last name" but date of birth doesn't show anything. http://prntscr.com/2miaa How can i activate mask plugin for only onfocus event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
亚尔...好吧,首先不要在标记中放入太多脚本。它很难阅读,而且更难以维护。让我们首先尝试分离表单的样式和功能。
编辑:可以在 http://jsfiddle.net/ninjascript/ 找到此操作的示例RuFHK/2/
CSS
HTML
JavaScript
唷...好吧,这更容易理解一点。现在您只想在焦点上发生一件事,那就是对 #dob 元素中包含的值应用掩码。这是编辑后的块:
现在您的输入应该仅在获得焦点后被屏蔽。
如果您想确保在提交时返回某些值,您可以随时验证“模糊”上的内容。我真的不想为这个表单中的每个字段编写 if 语句,所以我只想映射每个字段名称及其默认值并参考:
现在我可以参考每当我需要用默认值重新填充该字段时,都会映射该地图。对于 DOB 字段,看起来输入掩码插件也有一个“取消掩码”选项。不幸的是,由于 mask 中的字符是 #dob 字段值的一部分,因此我们不能只检查空值。相反,我将使用正则表达式来检查仅包含空格和“.”的值。字符:
Yarr... Ok, first don't put so much script inside your markup. It's difficult to read, and it's going to be even more difficult to maintain. Lets try separating the style and functionality of your form first.
Edit: A working example of this can be found at http://jsfiddle.net/ninjascript/RuFHK/2/
CSS
HTML
JavaScript
phew... OK that's a bit easier to understand. Now you want just one more thing to happen on focus, and that's to apply a mask to the value contained in the #dob element. Here's the edited block:
Now your input should only be masked after it gains focus.
If you want to make sure that some value is returned on submit, you can always validate the content on 'blur'. I don't really feel like writing if statements for every field in this form, so I'm just going to map each field name and it's default value and refer to that:
Now I can just refer to that map whenever I need to repopulate the field with my default values. In the case of the DOB field, it looks like the input mask plugin also has an 'unmask' option. Unfortunately because the characters within the mask are part of the #dob field's value, we can't just check for an empty value. Instead I'm going to use a regular expression that checks for a value that consists of nothing but whitespace and '.' chars: