Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何清除文本输入字段中的值?_Javascript_Jquery - Fatal编程技术网

Javascript 如何清除文本输入字段中的值?

Javascript 如何清除文本输入字段中的值?,javascript,jquery,Javascript,Jquery,我的代码没有运行,我希望在单击其他代码时清除该值 我做错了什么 您正在使用“占位符文本”作为选择器,它不会选择任何内容。使用$('input')。焦点(…和$('input')。模糊(…) 更新 如果要存储现有值,然后替换它,请将其作为.data()存储在元素本身中: $('input').focus(function() { $this = $(this); $this.data('oldval',$this.val()); $this.val(''); }); $

我的代码没有运行,我希望在单击其他代码时清除该值

我做错了什么


您正在使用“占位符文本”作为选择器,它不会选择任何内容。使用
$('input')。焦点(…
$('input')。模糊(…

更新

如果要存储现有值,然后替换它,请将其作为
.data()
存储在
元素本身中:

$('input').focus(function() {
    $this = $(this);
    $this.data('oldval',$this.val());
    $this.val('');
});
$('input').blur(function() {
    $this = $(this);
    $this.val($this.data('oldval'));
});​

或者可能:

$('input').blur(function() {
    $this = $(this);
    if ($this.val().length==0) { // only if the field is blank
        $this.val($this.data('oldval'));
    };
});​

您正在使用“占位符文本”作为选择器,它不会选择任何内容。请使用
$('input')。焦点(…
$('input')。模糊(…

更新

如果要存储现有值,然后替换它,请将其作为
.data()
存储在
元素本身中:

$('input').focus(function() {
    $this = $(this);
    $this.data('oldval',$this.val());
    $this.val('');
});
$('input').blur(function() {
    $this = $(this);
    $this.val($this.data('oldval'));
});​

或者可能:

$('input').blur(function() {
    $this = $(this);
    if ($this.val().length==0) { // only if the field is blank
        $this.val($this.data('oldval'));
    };
});​
你是说像这样吗

你是说像这样吗


这至少可以删除文本:

var placeholderText = $('input');

placeholderText.focus(function() {
    $(this).attr('value', "");
});
placeholderText.blur(function() {
    $(this).attr('value', phTextVal);
});​

您还可以看看HTML5。

这至少可以让文本删除工作正常:

var placeholderText = $('input');

placeholderText.focus(function() {
    $(this).attr('value', "");
});
placeholderText.blur(function() {
    $(this).attr('value', phTextVal);
});​

你也可以看看HTML5。我想更好的方法是:

var placeholderText = $('input');

$(placeholderText).focus(function() {
    phTextVal = $(this).val();
    $(this).val("");
}).blur(function() {
    $(this).val(phTextVal);
});​

我想更好的办法是:

var placeholderText = $('input');

$(placeholderText).focus(function() {
    phTextVal = $(this).val();
    $(this).val("");
}).blur(function() {
    $(this).val(phTextVal);
});​
看看吧

$('input').focus(function() {
    placeholderText = $('input').val();
    $(this).val("");
});

$('input').blur(function() {
    $(this).val(placeholderText);
});​

您仍然需要对输入的数据执行某些操作。

检查它

$('input').focus(function() {
    placeholderText = $('input').val();
    $(this).val("");
});

$('input').blur(function() {
    $(this).val(placeholderText);
});​


您仍然需要对输入的数据执行一些操作。

使用object.defaultValue属性如何

功能:

$.fn.smartInput=function(){
    return this.each(function(i,o){
     $(o).focus(function(){ if(this.value==this.defaultValue) this.value='';})
         .blur(function(){ this.value=(this.value=='')?this.defaultValue:this.value;});
     });
$('input').smartInput();
}

用法:

$.fn.smartInput=function(){
    return this.each(function(i,o){
     $(o).focus(function(){ if(this.value==this.defaultValue) this.value='';})
         .blur(function(){ this.value=(this.value=='')?this.defaultValue:this.value;});
     });
$('input').smartInput();

使用object.defaultValue属性怎么样

功能:

$.fn.smartInput=function(){
    return this.each(function(i,o){
     $(o).focus(function(){ if(this.value==this.defaultValue) this.value='';})
         .blur(function(){ this.value=(this.value=='')?this.defaultValue:this.value;});
     });
$('input').smartInput();
}

用法:

$.fn.smartInput=function(){
    return this.each(function(i,o){
     $(o).focus(function(){ if(this.value==this.defaultValue) this.value='';})
         .blur(function(){ this.value=(this.value=='')?this.defaultValue:this.value;});
     });
$('input').smartInput();

我这样做,但现在一切都显示为我的第一个值属性。可修复的,但我不明白为什么你想改写用户的文本,一旦他模糊了外地。我已经编辑了你的代码,但如果我想保持原来的占位符文本,我不知道如何做到这一点@ WebDeWal妞考虑更新的答案,或张贴一个新的问题。但现在一切都显示为我的第一个值属性。可修复的,但我不明白为什么你想改写用户的文本,一旦他模糊了外地。我已经编辑了你的代码,但如果我想保持原来的占位符文本,我不知道如何做到这一点@ WebDeV妞考虑更新的答案,或张贴一个新的问题。是的,我知道。html5占位符属性会自动完成,但我想学习jQuery方式是的,我知道html5占位符属性会自动完成,但我想学习jQuery方式