gpt4 book ai didi

javascript - 删除 :focus/control generated error field 上的输入文本

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:36 25 4
gpt4 key购买 nike

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

fiddle :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:正如评论中的人所说,这取决于浏览器。根据latest W3C spec draft :

User agents should present this hint to the user [...] e.g. by displaying it inside a blank unfocused control and hiding it otherwise.

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

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

广告。问题 2:这已被回答过几次,例如here .请查看代码的改进版本 here on 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.validate 插件附加到 fiddle 上。

关于javascript - 删除 :focus/control generated error field 上的输入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24788437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com