删除:focus/ control生成错误字段上的输入文本

Remove input text on:focus/ control generated error field

本文关键字:字段 输入 文本 错误 focus control 删除      更新时间:2023-09-26

好的,这是一个两部分的问题

小提琴:http://jsfiddle.net/aujqU/575/

问题1:我这里有一个表单,当我选择字段时,它不会删除输入文本。我想可能是"占位符"标签。然后我在这里阅读了其他与"占位符"相关的问题,并发现它属于HTML5功能,但显然这在这里不起作用。我使用jQuery验证的形式。

问题2:当用户没有输入所需的信息时,jQuery将生成一个"label:error"字段,可以按照我想要的样式设置。当表单填写不当时,它不会填充错误字段。它只是给出浏览器的默认值。任何想法. . ?

<div id="form-frame">
     <h3>Schedule a Free Security Review</h3>
    <p>Fill out the form and a Security Specialist will contact you at the phone number below about offers.</p>
    <p>All fields required.</p>
    <form id="contactForm" method="POST" action="">
        <input type="text" id="first_name" name="first_name" minlength="5" maxlength="255" placeholder="First Name" value="" required />
        <input type="text" id="last_name" name="last_name" minlength="2" maxlength="255" placeholder="Last Name" value="" required />
        <input type="hidden" id="state" name="state" value="" />
        <input type="text" id="email" name="email" minlength="25" maxlength="255" placeholder="Email" value="" required />
        <input type="text" id="postal_code" name="postal_code" minlength="5" maxlength="10" placeholder="Zip Code" value="" required />
        <input type="text" id="phone_primary" name="phone_primary" minlength="11" maxlength="12" placeholder="Phone" value="" required />
        <input type="submit" value="Submit" />
    </form>
     <h6>* I agree to receive telemarketing calls at the telephone number provided above.</h6>
    <p>Or Call Us At <span> 888-888-8888</span>
    </p>
</div>
 $('#contactForm').validate({
     onfocusout: function (element) {
         this.element(element);
     },
     rules: {
         first_name: 'required',
         last_name: 'required',
         state: 'required',
         email: 'required',
         postal_code: 'required',
         phone_primary: 'required'
     },
     messages: {
         first_name: 'Please enter your first name',
         last_name: 'Please enter your last name',
         email: 'Please enter your email',
         postal_code: 'Please enter your zip code',
         phone_primary: 'Please enter your phone number'
     }
 });

广告。问题1:正如评论中的家伙所说,这取决于浏览器。根据最新的W3C规范草案:

用户代理应该向用户显示这个提示[…]例如,将其显示在空白的未聚焦控件中,否则将其隐藏。

不幸的是,并不是所有的浏览器都遵循这个建议,例如最新版本的Chrome不隐藏占位符当一个字段被聚焦。当然,您可以使用Javascript来处理这个问题。

但是,请记住这是对占位符的一种滥用,因为(同样,根据规范)它们应该被用作提示,而不是标签。

广告。问题2:这个问题已经回答过几次了,例如这里。请在jsfiddle上查看您代码的改进版本:

// Problem no.2 - added http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.min.js to the "External resources" in jsfiddle and it started working as expected
$('#contactForm').validate({
     onfocusout: function (element) {
         this.element(element);
     },
     rules: {
         first_name: 'required',
         last_name: 'required',
         state: 'required',
         email: 'required',
         postal_code: 'required',
         phone_primary: 'required'
     },
     messages: {
         first_name: 'Please enter your first name',
         last_name: 'Please enter your last name',
         email: 'Please enter your email',
         postal_code: 'Please enter your zip code',
         phone_primary: 'Please enter your phone number'
     }
 });
// Problem no. 1
$('input[type=text]').focus(function () {
    this['data-placeholder_old'] = this.placeholder;
    this.placeholder = '';
});
$('input[type=text]').blur(function () {
    this.placeholder = this['data-placeholder_old'] || '';
})

为了验证工作,我只附加了jquery。