Javascript 使用 jQuery 获取输入的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4591889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get default value of an input using jQuery
提问by Bato Dor
$(".box_yazi2").each(function () {
var default_value = this.value;
$(this).css('color', '#555'); // this could be in the style sheet instead
$(this).focus(function () {
if (this.value == default_value) {
this.value = '';
$(this).css('color', '#000');
}
});
$(this).blur(function () {
if (this.value == '') {
$(this).css('color', '#555');
this.value = default_value;
}
});
});
This function of default value of input doesnt work in FF, but perfectly works in IE and ofcourse the input itself looks like this:
这个输入默认值的功能在FF中不起作用,但在IE中完美运行,当然输入本身看起来像这样:
<input type="text" class="box_yazi2" id="konu" name="konu" value="Bo?" />
采纳答案by Andreas Niedermair
The solution is quite easy; you have an extra });
in your code (thanks @ Box9).
解决方案很简单;你});
的代码中有一个额外的(感谢@ Box9)。
I would encourage you to reuse the variable and not create dozens of jQuery objects.
我鼓励您重用该变量而不是创建数十个 jQuery 对象。
I've changed your example to background-color
but it will work.
我已将您的示例更改为,background-color
但它会起作用。
$('.box_yazi2').each(function(index, element) {
var $element = $(element);
var defaultValue = $element.val();
$element.css('background-color', '#555555');
$element.focus(function() {
var actualValue = $element.val();
if (actualValue == defaultValue) {
$element.val('');
$element.css('background-color', '#3399FF');
}
});
$element.blur(function() {
var actualValue = $element.val();
if (!actualValue) {
$element.val(defaultValue);
$element.css('background-color', '#555555');
}
});
});
回答by Salman A
Just use the defaultValue
property:
只需使用该defaultValue
属性:
var default_value = $(this).prop("defaultValue");
Or:
或者:
var default_value = this.defaultValue;
回答by lior amrosi
$('input[type="text"]').focus( function(){
elementValue = $(this).val();
$(this).val("");
});
$('input[type="text"]').blur( function(){
if($(this).val() != elementValue && $(this).val() != ""){
}else{
$(this).val(elementValue);
}
});
回答by Sirozha
I'm using the next code:
我正在使用下一个代码:
//clear the focused inputs
$('input[type="text"]').focus( function(){
if( $(this).attr('value') == $(this).attr('defaultValue') ){
$(this).attr('value', '');
};
} );
$('input[type="text"]').blur( function(){
if( $(this).attr('value') == '' ){
$(this).attr('value', $(this).attr('defaultValue') );
};
} );
回答by 23inhouse
Use this.defaultValue
用 this.defaultValue
Sorry for the link to w3notcools, http://www.w3schools.com/jsref/prop_text_defaultvalue.asp
抱歉链接到 w3notcools,http://www.w3schools.com/jsref/prop_text_defaultvalue.asp
回答by Wahab Qureshi
You should use prop instead of so many functions to be honest, use 'delegate' instead of 'on' for late static binding.
老实说,您应该使用 prop 而不是这么多函数,对于后期静态绑定使用“delegate”而不是“on”。
$('.box_yazi2').each(function() {
$(this).on('focus', function(){
if($(this).val() == $(this).prop('defaultValue')){
$(this).val('');
$(this).css('color', '#000');
}
});
$(this).on('blur', function(){
if($(this).val() == ''){
$(this).val($(this).prop('defaultValue'));
$(this).css('color', '#000');
}
});
});