如何删除焦点输入的默认值

发布于 2024-09-25 02:47:34 字数 166 浏览 0 评论 0原文

我有一个输入框,分配有默认值文本。当用户聚焦于字段时如何删除此文本::

CoDE

<input type="text" name="kp1_description" value="Enter Keypress Description">

I have an input box that has default value text assigned to it. How can I remove this text when the user focuses on the field::

CoDE

<input type="text" name="kp1_description" value="Enter Keypress Description">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

烂人 2024-10-02 02:47:34
$(document).ready(function(){
    var Input = $('input[name=kp1_description]');
    var default_value = Input.val();

    Input.focus(function() {
        if(Input.val() == default_value) Input.val("");
    }).blur(function(){
        if(Input.val().length == 0) Input.val(default_value);
    });
})​

应该可以做到这一点。

更新了,忘记焦点没有焦点事件的第二个参数,因为没有,它必须与模糊链接:

http://jsfiddle.net/hDCsZ/

您还应该考虑为此创建自己的函数,例如:

$.fn.ToggleInputValue = function(){
    return $(this).each(function(){
        var Input = $(this);
        var default_value = Input.val();

        Input.focus(function() {
           if(Input.val() == default_value) Input.val("");
        }).blur(function(){
            if(Input.val().length == 0) Input.val(default_value);
        });
    });
}

然后像这样使用

$(document).ready(function(){
    $('input').ToggleInputValue();
})​
$(document).ready(function(){
    var Input = $('input[name=kp1_description]');
    var default_value = Input.val();

    Input.focus(function() {
        if(Input.val() == default_value) Input.val("");
    }).blur(function(){
        if(Input.val().length == 0) Input.val(default_value);
    });
})​

That should do it.

Updated, Forgot that focus does not have a 2nd parameter for the focus-out event because there is none, it has to be chained with blur:

http://jsfiddle.net/hDCsZ/

you should also think about creating your own function for this such as:

$.fn.ToggleInputValue = function(){
    return $(this).each(function(){
        var Input = $(this);
        var default_value = Input.val();

        Input.focus(function() {
           if(Input.val() == default_value) Input.val("");
        }).blur(function(){
            if(Input.val().length == 0) Input.val(default_value);
        });
    });
}

Then use like so

$(document).ready(function(){
    $('input').ToggleInputValue();
})​
醉生梦死 2024-10-02 02:47:34

不要这样做。使用 jQuery 水印脚本:
http://code.google.com/p/jquery-watermark/

$("input[name='kp1_description']").watermark("输入按键描述");

如果您手动执行此操作,则必须考虑很多事情。例如,当文本框失去焦点时会发生什么?如果没有价值,您需要读取帮助文本。如果有,您会希望尊重这些更改。

让其他人做繁重的工作更容易:)

Don't do it this way. Use a jQuery watermark script:
http://code.google.com/p/jquery-watermark/

$("input[name='kp1_description']").watermark("Enter Keypress Description");

There are a lot of things you have to account for if you do it manually. For instance, what happens when the text box loses focus? If there's no value, you'd want to readd your helper text. If there is, you'd want to honor those changes.

Just easier to let other people do the heavy lifting :)

蝶…霜飞 2024-10-02 02:47:34

使用 HTML5,您无需 Javascript 即可完成此操作:只需使用 placeholder 而不是 值。
我认为这是一个很好的补充,但当然您需要首先检查浏览器兼容性。

With HTML5 you could do it without Javascript: just use placeholder instead of value.
I think it's a nice addition, but of course you need to check the browser compatibility first.

浅紫色的梦幻 2024-10-02 02:47:34
<input type="text" id="anything" placeholder="enter keypress description">

我认为这会有所帮助

<input type="text" id="anything" placeholder="enter keypress description">

i think this will help

我也只是我 2024-10-02 02:47:34
$('input, textarea').each(function () {
    var Input = $(this);
    var default_value = Input.val();

    Input.focus(function() {
        if(Input.val() == default_value) Input.val("");
    }).blur(function(){
        if(Input.val().length == 0) Input.val(default_value);
    });
});
$('input, textarea').each(function () {
    var Input = $(this);
    var default_value = Input.val();

    Input.focus(function() {
        if(Input.val() == default_value) Input.val("");
    }).blur(function(){
        if(Input.val().length == 0) Input.val(default_value);
    });
});
握住我的手 2024-10-02 02:47:34

var defaultForInput = "abc";


提交表单时,检查是否输入( id="myTextInput") 值是“空字符串”,如果是这样,请将其替换为 (defaultForInput)。

var defaultForInput = "abc";

<input id="myTextInput" type="text" placeholder=defaultForInput />


When submitting your form, check if the input(id="myTextInput") value is the 'empty-string', if so substitute it with (defaultForInput).

小忆控 2024-10-02 02:47:34

超级骗子短版

$('#input_id').focus(function() {
    $(this).val("");
});

The Super Duper Short Version

$('#input_id').focus(function() {
    $(this).val("");
});
咋地 2024-10-02 02:47:34

HTML:

<form method="post">
  <input type="text" id="customer" value="Username"/>      
  <input type="Submit" value="Login" class="rc" />
</form>

Javascript 代码:

<script>
$('#customer').on({
    focus:function(){                   
      if(this.value == 'Username') this.value = '';
    },
    blur:function(){
      if(this.value == '') this.value = 'Username';
    }
})
</script>

就这些,希望对您有所帮助。

HTML:

<form method="post">
  <input type="text" id="customer" value="Username"/>      
  <input type="Submit" value="Login" class="rc" />
</form>

Javascript code:

<script>
$('#customer').on({
    focus:function(){                   
      if(this.value == 'Username') this.value = '';
    },
    blur:function(){
      if(this.value == '') this.value = 'Username';
    }
})
</script>

That's all, I hope it'll help.

等风来 2024-10-02 02:47:34

普通 JavaScript:

(function () {
  let input = document.querySelector ("input[name='kp1_description']");
  input.onfocus = function () {
    this.placeholder = this.value;
    this.value = '';
  };
})();

这会将值保留在占位符中,而不是完全删除它。如果您不喜欢这样,请删除占位符行。

Plain JavaScript:

(function () {
  let input = document.querySelector ("input[name='kp1_description']");
  input.onfocus = function () {
    this.placeholder = this.value;
    this.value = '';
  };
})();

This keeps the value in the placeholder instead of removing it completely. If you do not like this, remove the placeholder line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文