简体   繁体   English

清除多个输入的值?

[英]Clearing Value on Multiple Inputs?

I have a jQuery script below that clears the value of an input on focus and puts it back on blur if the input remains empty. 我下面有一个jQuery脚本,可清除焦点输入的值,如果输入为空,则将其放回模糊状态。 It works great, but here's the problem: 效果很好,但这是问题所在:

Let's say I have 3 input fields on the same page. 假设我在同一页面上有3个输入字段。 I click on the first one with the value "Name", defaultValue variable becomes "Name" and the field clears. 我单击第一个值为“名称”的值, defaultValue变量变为“名称”,并且该字段清除。 If I click out, the value goes back to "Name". 如果单击,该值将返回到“名称”。 Now if I click on the second field without refreshing the page, it clears just fine, but when I click outside, instead of getting the value "Initials", it gets the value of the first field. 现在,如果我在不刷新页面的情况下单击第二个字段,则清除就可以了,但是当我在外部单击时,不是获取值“ Initials”,而是获取第一个字段的值。

So how can I get the defaultValue variable to update itself every time I click on a field? 那么,每次单击字段时,如何获取defaultValue变量进行更新?

var adaugaInput = $('form#adauga input:text');

adaugaInput.focus(function() {
    var defaultValue = $(this).val();
    if($(this).attr("value") == defaultValue) $(this).attr("value", "");
    adaugaInput.blur(function() {
        if($(this).attr("value") == "") $(this).attr("value", defaultValue);
    });
});

The explanation for why your code works the way it does is kind-of complicated. 关于代码为何按其工作方式的解释有些复杂。 For every input field, you're establishing (and re-establishing, on every "focus" event!) handlers for "blur" events on all the inputs. 对于每个输入字段,您都将为所有输入上的“模糊”事件建立(并重新建立每个“焦点”事件!)处理程序。 It's confusing and hard to think about, so I'll just sum it up and say "don't do it that way." 这令人困惑且难以思考,因此我将其总结为“不要那样做”。

adaugaInput.focus(function() {
  var input = $(this);
  if (!input.data('default')) input.data('default', input.val());
  if (input.val() === input.data('default'))
    input.val('');
});

adaugaInput.blur(function() {
  var input = $(this);
  if (input.val() === '') input.val(input.data('default'));
});

Note that I use ".val()" to get/set the "value" attribute of the input fields. 请注意,我使用“ .val()”来获取/设置输入字段的“值”属性。 Also, the "blur" handler is set up outside the "focus" handler. 此外,“模糊”处理程序设置了“焦点访谈”的处理程序之外 This code uses the jQuery ".data()" mechanism to keep the per-element default value. 这段代码使用jQuery“ .data()”机制来保留每个元素的默认值。 Not tested but it's probably pretty close. 未经测试,但可能非常接近。

With a mechanism like this, it's sometimes nice to re-style the input so that (for example) the default value shows up with a lighter font color. 使用这样的机制,有时最好重新设置输入的样式,以便(例如)使用较浅的字体颜色显示默认值。 To do that, you'd remove and add a class to the input field, and affect the style with CSS. 为此,您将删除一个类并将其添加到输入字段,并使用CSS影响样式。 (The inputs would have to start off with the class; or I suppose you could apply the class on focus.) (输入必须从班级开始;或者我想您可以将班级放在重点上。)

i usually use this code which works :) 我通常使用此代码有效:)

http://www.eggchops.com/web-stuff/jquery/jquery-clear-focus-function/ http://www.eggchops.com/web-stuff/jquery/jquery-clear-focus-function/

here's the code: 这是代码:

$(document).ready(function(){

var clearMePrevious = ”;

// clear input on focus
$(’.clearMeFocus’).focus(function() {
  if($(this).val()==$(this).attr(’title’)) {
    clearMePrevious = $(this).val();
    $(this).val(”);
  }
});

// if field is empty afterward, add text again
$(’.clearMeFocus’).blur(function() {
  if($(this).val()==”) {
    $(this).val(clearMePrevious);
  }
});
});

I'd go the other way around, saving defaultValue first, then binding focus: 我会反过来,先保存defaultValue,然后绑定焦点:

var adaugaInput = $('form#adauga input:text');

adaugaInput.each(function(){
    var defaultValue = $(this).val();
        $(this).focus(function() {

        if($(this).attr("value") == defaultValue) $(this).attr("value", "");
    });
    $(this).blur(function() {
        if($(this).attr("value") == "") $(this).attr("value", defaultValue);
    });
});

Standard disclaimers apply, the above code is not tested. 适用标准免责声明,以上代码未经测试。 (edit: fixed mulitple blur binds) (编辑:修复了多个模糊绑定)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM