表单输入onfocus问题

发布于 2024-11-29 15:36:14 字数 1148 浏览 0 评论 0原文

我想在我的注册表中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野心澎湃 2024-12-06 15:36:14

亚尔...好吧,首先不要在标记中放入太多脚本。它很难阅读,而且更难以维护。让我们首先尝试分离表单的样式和功能。

编辑:可以在 http://jsfiddle.net/ninjascript/ 找到此操作的示例RuFHK/2/

CSS

input { color: #aea592; }
input.default { font-style: italic; }

HTML

<input class="part1 default" type="text" name="lname" value="Last Name"/>
<input class="part2 default" type="text" id="dob" name="dob" value="Date of Birth"/>

JavaScript

$('input').bind('focus', function() {
    var el = $(this);
    if (el.hasClass('default')) {
        el.removeClass('default').val('');
    }
});

...好吧,这更容易理解一点。现在您只想在焦点上发生一件事,那就是对 #dob 元素中包含的值应用掩码。这是编辑后的块:

$('input').bind('focus', function() {
    var el = $(this);
    if (el.hasClass('default')) {
        el.removeClass('default').val('');
    }
    if (el.attr('id') === 'dob') {
        $(this).mask('99.99.9999', {placeholder:' '});
    }
});

现在您的输入应该在获得焦点后被屏蔽。

如果您想确保在提交时返回某些值,您可以随时验证“模糊”上的内容。我真的不想为这个表单中的每个字段编写 if 语句,所以我只想映射每个字段名称及其默认值并参考:

var defaultValues = {
    'lname': 'Last Name',
    'dob': 'Date of Birth'
};

现在我可以参考每当我需要用默认值重新填充该字段时,都会映射该地图。对于 DOB 字段,看起来输入掩码插件也有一个“取消掩码”选项。不幸的是,由于 mask 中的字符是 #dob 字段值的一部分,因此我们不能只检查空值。相反,我将使用正则表达式来检查仅包含空格和“.”的值。字符:

$('input').bind('blur', function() {
    var el = $(this);
    var name = el.attr('name');

    // Really we only want to do anything if the field is *empty*
    if (el.val().match(/^[\s\.]*$/)) {

        // To get our default style back, we'll re-add the classname
        el.addClass('default');

        // Unmask the 'dob' field
        if (name === 'dob') {
            el.unmask();
        }

        // And finally repopulate the field with its default value
        el.val(defaultValues[name]);
    }
});

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

input { color: #aea592; }
input.default { font-style: italic; }

HTML

<input class="part1 default" type="text" name="lname" value="Last Name"/>
<input class="part2 default" type="text" id="dob" name="dob" value="Date of Birth"/>

JavaScript

$('input').bind('focus', function() {
    var el = $(this);
    if (el.hasClass('default')) {
        el.removeClass('default').val('');
    }
});

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:

$('input').bind('focus', function() {
    var el = $(this);
    if (el.hasClass('default')) {
        el.removeClass('default').val('');
    }
    if (el.attr('id') === 'dob') {
        $(this).mask('99.99.9999', {placeholder:' '});
    }
});

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:

var defaultValues = {
    'lname': 'Last Name',
    'dob': 'Date of Birth'
};

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:

$('input').bind('blur', function() {
    var el = $(this);
    var name = el.attr('name');

    // Really we only want to do anything if the field is *empty*
    if (el.val().match(/^[\s\.]*$/)) {

        // To get our default style back, we'll re-add the classname
        el.addClass('default');

        // Unmask the 'dob' field
        if (name === 'dob') {
            el.unmask();
        }

        // And finally repopulate the field with its default value
        el.val(defaultValues[name]);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文