清除移动网站表单字段元素上的默认文本
好的,我正在使用 jQuery Mobile 框架,我希望能够在文本或文本中使用默认值区域作为辅助文本。但在焦点上清除文本并保留新输入的值。还要重新聚焦(假设他们再次错误地触摸它),不要再次清除该值。
我包括这些
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js"></script>
示例不起作用
将其添加到页面本身:(链接到示例)
<script type="text/javascript">
$('.default-value').each(function() {
var default_value = this.value;
$(this).focus(function() {
if(this.value == default_value) {
this.value = '';
}
});
$(this).blur(function() {
if(this.value == '') {
this.value = default_value;
}
});
});
</script>
这是 HTML(它也在表单标签中)
<!-- Address 2 -->
<div data-role="fieldcontain">
<label for="address-2">Address 2</label>
<input type="text" name="address-2" id="address-2" class="default-value" value="Apt #, Suite #, Floor #" />
</div>
Ok I'm using the jQuery Mobile framework and I would like to be able to use the default values in a text or text area as helper text. But on focus clear the text and keep the newly entered value. Also on re-focus (say they touch it again by mistake) not to clear the value again.
I'm including these
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js"></script>
The example is not working
Adding this to the page itself: (link to example)
<script type="text/javascript">
$('.default-value').each(function() {
var default_value = this.value;
$(this).focus(function() {
if(this.value == default_value) {
this.value = '';
}
});
$(this).blur(function() {
if(this.value == '') {
this.value = default_value;
}
});
});
</script>
Here is the HTML (It's in a form tag as well)
<!-- Address 2 -->
<div data-role="fieldcontain">
<label for="address-2">Address 2</label>
<input type="text" name="address-2" id="address-2" class="default-value" value="Apt #, Suite #, Floor #" />
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于后代:HTML5 中有一种称为占位符的东西
当前最新的基于 webkit 的浏览器支持占位符。
For future generations: there's a thing called placeholder in HTML5
Placeholder is currently supported in newest webkit-based browsers.
我能够使用 jQuery 1.5 和 jQuery Mobile 1.0a3 让您的代码正常工作。您是否包含 jQuery 以及 jQuery Mobile? jQuery Mobile 需要 jQuery 版本才能工作。
编辑:我应该注意,我只在 iPhone 模拟器上测试过这个,而不是在实际设备上。模拟器运行的是 iOS 4.0.1。
I was able to get your code to work using jQuery 1.5 and jQuery Mobile 1.0a3. Did you include jQuery as well as jQuery Mobile? jQuery Mobile requires a version of jQuery to work.
Edit: I should note that I only tested this on the iPhone simulator, and not an actual device. The simulator was running iOS 4.0.1.
进一步到 HTML5 的输入占位符属性,其行为与您描述的帮助文本完全相同; jQuery Mobile 建议使用带有“ui-hidden-accessible”类的标签字段:
http://jquerymobile.com/ demos/1.1.0/docs/forms/docs-forms.html
然后,您可以使用 Modernizr 来检测对占位符属性的支持,如果不支持,请使用 polyfill (像这个)或删除将显示标签的 ui-hidden-accessible 类。
然后根据需要设置新标签的样式。我发现当输入字段为 type="password" 时,polyfill 方法效果不佳,因此在这种情况下取消隐藏标签会更好。
Further to HTML5's input placeholder attribute, which behaves exactly as the helper text you describe; jQuery Mobile recommends using the label field with the class "ui-hidden-accessible":
http://jquerymobile.com/demos/1.1.0/docs/forms/docs-forms.html
You can then use Modernizr to detect the support for the placeholder attribute and if it is not supported, use a polyfill (like this one) or remove the ui-hidden-accessible class which will display the labels.
then style the new label as desired. I've found the polyfill approach doesn't work well when the input field is type="password", so unhiding the label is better in that case.