jQuery输入值切换

发布于 2024-10-17 00:36:19 字数 854 浏览 2 评论 0原文

我一直在使用这段代码来切换焦点和模糊输入的值:

$('input, textarea').focus(function() {
            value=$(this).val();
            $(this).attr("value","")};
        });
        $('input, textarea').blur(function() {
            if($(this).val()=="") {
                $(this).val(value);
            }
        });

唯一的问题是,当我在这些输入中输入一些内容,然后在输入另一个输入后重新聚焦时,该字段会变成空白。有没有办法编辑这段代码来抑制这种情况?

这里为您提供示例 :)


我尝试了这样的方法,但它没有达到我想要的效果:

$('input, textarea').focus(function() {
            value=$(this).val();
    if($(this).val(value)) {
            $(this).attr("value","");
        });
        $('input, textarea').blur(function() {
            if($(this).val()=="") {
                $(this).val(value);
            }
        });

I've been using this code to switch the value of my inputs onFocus and Blur:

$('input, textarea').focus(function() {
            value=$(this).val();
            $(this).attr("value","")};
        });
        $('input, textarea').blur(function() {
            if($(this).val()=="") {
                $(this).val(value);
            }
        });

Only problem is, when I type something into these inputs, then refocus after typing into another, the field goes blank. Is there a way to edit this code to inhibit this?

Example for you here :)


I tried something like this, but it didn't have the effect I desire:

$('input, textarea').focus(function() {
            value=$(this).val();
    if($(this).val(value)) {
            $(this).attr("value","");
        });
        $('input, textarea').blur(function() {
            if($(this).val()=="") {
                $(this).val(value);
            }
        });

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

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

发布评论

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

评论(5

合久必婚 2024-10-24 00:36:19

您需要设置初始值并进行检查。

$('input, textarea').each(function(){
    // initial storing of the pre-defined value
    $(this).data('initial', this.value);
}).focus(function(){
    var $this = $(this);
    // on focus if the value of the field is still the initial then clear it
    if ( (this.value) == $this.data('initial') )
      this.value = '';
}).blur(function(){
    var $this = $(this);
    // on blur if the value is still cleared then restore the initial 
    if ( (this.value) == '' )
        this.value = $this.data('initial');
})

演示http:// jsfiddle.net/EMYmG/2/


更新

您可以避免存储到数据,因为我们可以使用一个名为 defaultValue 的标准属性。 (变得更加清晰

$('input, textarea').focus(function(){
        // on focus if the value of the field is still the initial then clear it
        if ( (this.value) == this.defaultValue )
          this.value = '';
    }).blur(function(){
        // on blur if the value is still cleared then restore the initial 
        if ( (this.value) == '' )
            this.value = this.defaultValue;
    })

演示http://jsfiddle.net /PjsRQ/

You need to set the initial value and check against that..

$('input, textarea').each(function(){
    // initial storing of the pre-defined value
    $(this).data('initial', this.value);
}).focus(function(){
    var $this = $(this);
    // on focus if the value of the field is still the initial then clear it
    if ( (this.value) == $this.data('initial') )
      this.value = '';
}).blur(function(){
    var $this = $(this);
    // on blur if the value is still cleared then restore the initial 
    if ( (this.value) == '' )
        this.value = $this.data('initial');
})

Demo: http://jsfiddle.net/EMYmG/2/


Update

You can avoid storing to the data, since there is a standard property named defaultValue that we can use instead. (gets much cleaner)

$('input, textarea').focus(function(){
        // on focus if the value of the field is still the initial then clear it
        if ( (this.value) == this.defaultValue )
          this.value = '';
    }).blur(function(){
        // on blur if the value is still cleared then restore the initial 
        if ( (this.value) == '' )
            this.value = this.defaultValue;
    })

Demo : http://jsfiddle.net/PjsRQ/

二智少女 2024-10-24 00:36:19

值不是标签。试图假装它们会导致严重的可访问性问题。

如果你真的想使用这种设计,我劝你不要这样做。然后将标签放置在输入下方并设置它们的样式,以便它们不可见(但仍然存在于屏幕阅读器的 DOM 中)。

jQuery 示例在我的网站上,代码很清晰,但是它正在等待正确记录。

Values are not labels. Trying to pretend they are leads to major accessibility problems.

If you really want to use this sort of design, and I urge you not to. Then position labels under the inputs and style them so they can't be seen (but still exist in the DOM for screen readers).

A jQuery example is on my website, the code is clear, but it is waiting to be properly documented.

(り薆情海 2024-10-24 00:36:19

尝试这样的事情: http://jsfiddle.net/Ajcmq/

这是一件非常简单的事情,真的:如果该字段的值是最初的四个值之一,然后清除它,否则,我们假设它已被更改,并且我们将其保留在焦点上。

唯一的缺点是,当您在另一个字段中使用四个默认值之一时,它也会被清空:D。

Try something like this: http://jsfiddle.net/Ajcmq/

It's a very simple thing, really: if the field's value is one of the initial four, then clear it, otherwise, we assume it's been changed and we leave it alone on focus.

The only drawback is that when you use one of the four dfault values in another field, it will be emptied as well :D.

骄兵必败 2024-10-24 00:36:19

jQuery 表单示例插件 执行此操作的方式是:为输入元素分配一个类,并在元素中的初始值更改后将其删除,以确定哪些元素在 DOM 加载后已被编辑以及哪些元素仍包含初始值。

The way the jQuery Form Example plugin does this is by assigning a class to the input elements and removing it after the initial value in the elements has been changed to determine which elements have been edited after the DOM load and which still contain the initial value.

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