Javascript 删除:焦点/控件生成的错误字段上的输入文本
好的,这是一个两部分的问题 小提琴: 问题1: 我这里有一个表单,当我选择字段时,它不会删除输入文本。我想可能是“占位符”标签。然后我在这里阅读了其他关于“占位符”的问题,发现它是HTML5函数的属性,但显然这在这里不起作用。我正在使用jQuery验证表单 问题2: 当用户未输入所需信息时,jQuery将生成一个“label:error”字段,该字段可以按照我的要求设置样式。当表单填写不正确时,它不会填充错误字段。它只是给出浏览器的默认值。有什么想法吗Javascript 删除:焦点/控件生成的错误字段上的输入文本,javascript,jquery,html,css,Javascript,Jquery,Html,Css,好的,这是一个两部分的问题 小提琴: 问题1: 我这里有一个表单,当我选择字段时,它不会删除输入文本。我想可能是“占位符”标签。然后我在这里阅读了其他关于“占位符”的问题,发现它是HTML5函数的属性,但显然这在这里不起作用。我正在使用jQuery验证表单 问题2: 当用户未输入所需信息时,jQuery将生成一个“label:error”字段,该字段可以按照我的要求设置样式。当表单填写不正确时,它不会填充错误字段。它只是给出浏览器的默认值。有什么想法吗 <div id="form-fram
<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'
}
});
安排一次免费的安全审查
填写表格后,安全专家将通过下面的电话号码与您联系,了解有关优惠信息
所有字段都是必需的
*我同意接受上述电话号码的电话销售电话。
或致电888-888-8888与我们联系
$(“#contactForm”)。验证({
onfocusout:函数(元素){
这个,元素(元素),;
},
规则:{
第一个名称:“必需”,
姓氏:“必选”,
声明:“必需”,
电子邮件:“必需”,
邮政编码:“必填”,
主要电话:'必选'
},
信息:{
名字:“请输入您的名字”,
姓氏:“请输入您的姓氏”,
电子邮件:“请输入您的电子邮件”,
邮政编码:'请输入您的邮政编码',
phone_primary:'请输入您的电话号码'
}
});
广告问题1:正如评论中的人所说,这取决于浏览器。根据报告:
用户代理应向用户[…]显示此提示,例如,将其显示在空白的非焦点控件中,然后以其他方式隐藏
不幸的是,并非所有浏览器都遵循这一建议,例如,最新版本的Chrome在聚焦某个字段时不会隐藏占位符。当然,您可以使用Javascript来处理这个问题
但是,请记住,这有点滥用占位符,因为(同样,根据规范)占位符应该用作提示,而不是标签
问题2:这个问题已经回答了好几次,例如。请查看代码的改进版本:
为了验证工作,我只将jquery.validate插件附加到小提琴上。对于问题1:占位符不会消失
onfocus
。当你开始打字时,它将消失。(A)你有两个独立的问题在这里-你应该把它们作为两个问题发布。(B) 占位符的行为取决于浏览器。浏览器一有焦点就可以清除字段,也可以等到你开始打字后再清除。先生,你是一位绅士和学者。认可的。完美的非常感谢。
// 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'] || '';
})