删除输入文本:焦点/控制生成的错误字段

时间:2014-07-16 18:49:45

标签: javascript jquery html html5 css3

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

FIDDLE:http://jsfiddle.net/aujqU/575/

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

问题2: 当用户没有输入所需信息时,jQuery将生成一个"标签:错误"字段可以设置我想要的样式。当表单填写不正确时,它不会填充错误字段。它只是提供了浏览器的默认值。有什么想法..?

<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 个答案:

答案 0 :(得分:1)

广告。问题1 :正如评论中的人所说,这取决于浏览器。根据{{​​3}}:

  

用户代理应将此提示提供给用户[...],例如通过将其显示在空白的未聚焦控件中并以其他方式隐藏它。

不幸的是,并非所有浏览器都遵循此建议,例如当字段聚焦时,最新版本的Chrome不会隐藏占位符。当然,你可以使用Javascript处理它。

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

广告。问题2 :已经回答了几次,例如latest W3C spec draft。请查看代码here的改进版本:

// 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.validate插件附加到小提琴。