删除:focus/ control生成错误字段上的输入文本
Remove input text on:focus/ control generated error field
好的,这是一个两部分的问题
小提琴: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。
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 而循环只设置php中输入字段中的第一个值
- 使用 Javascript 将字段输入除以 12
- 合并来自多个字段输入的搜索结果
- 使用 Javascript 实时打印字段输入数据
- 防止AngularJS中跨路由的字段输入重复
- 引用Iframe中的字段输入
- 禁用html5”;“必需”;不显示窗体部分时的字段输入属性
- JavaScriptFormValidation-需要特定的字段输入,但使用大写/小写
- 验证VF页面上所需字段输入的Javascript不起作用
- 在没有
- 使用Jquery将字段输入值作为查询字符串附加到url
- HTML5 jQuery选择所有日期字段'输入:date'
- 将字段输入复制到另一个字段输入
- 向图像添加字段(输入)
- 动态添加字段输入不会存储值并传递给控制器
- 向输入字段输入键击
- 我如何通过javascript验证datetime字段输入
- HTML如何将用户字段输入作为json对象发送
- 如何根据'select' onchange事件将值填充到第二个输入字段?输入