我应该如何实现这个功能?如果用户留空了字段,我想要获得原始值。
这是我目前的代码:Jsfiddle Demo。
这是我目前的代码:Jsfiddle Demo。
$(document).ready(function() {
var field = $('input[type="text"]');
field.focus(function() { //Empty the field on focus
var thisValue = $(this).val();
$(this).attr("value", "");
});
field.blur(function() { //Check the field if it is left empty
if ($(this).val() == "") {
//alert('This field can not be left empty');
$(this).val(thisValue);
}
});
});
:text
选择器的性能较差:“因为 :text 是 jQuery 的扩展,而不是 CSS 规范的一部分,使用 :text 进行查询无法利用本机 DOM querySelectorAll() 方法提供的性能提升。为了获得更好的现代浏览器性能,请改用 [type="text"]。” - gdoron.focusout
和.blur
之间有区别。 - eric.dummy