简体   繁体   English

使用jQuery隐藏/显示Input的值文本并进行模糊处理

[英]Hide/Show value text of Input on focus and blur using jQuery

I have value attribute of an input. 我有输入的value属性。 I need it to disappear on focus for that particular textbox, and reappear on blur, if the textbox has no content. 如果文本框没有内容,我需要使它消失在焦点上,并重新显示在特定的文本框上。

This is my current HTML code. 这是我当前的HTML代码。

 <label>First Name</label></td>
 <input class="required" type="text" name="firstname" value="Fill" id="firstname" /><span>(required)</span>

Similarly i tried one other thing.When focus is in(Textbox A),a new textbox(B) appears and when focus is out(From A),B gets disappeared.But here is something wrong.I need the user to enter something in Box B also.Now i need something this.When focus is out from A as well B,only then B should disappear. 同样,我尝试了另一件事。当焦点位于(文本框A)中时,会出现一个新的文本框(B),而当焦点不在(从A)中时,B消失了。但是这里出了点问题。我需要用户输入现在我还需要一些东西。当焦点同时从A和B移出时,只有B消失了。 Note:There are number of text box on the page. 注意:页面上有多个文本框。 This is Code for it. 这就是它的代码。

$('#lastname').focusin(function () {
        $('input.hidden').fadeIn(1000);
        $('input.hidden').css('backgroundColor', 'yellow');
        }).focusout(function () {
        $('input.hidden').hide();
        });

Thanks in advance. 提前致谢。

​$("#firstname")​.on({
    focus: function() {
        if (this.value==='Fill') this.value = '';
    },
    blur: function() {
        if (this.value==='') this.value = 'Fill';
    }
})​;​

FIDDLE 小提琴

If older browsers are'nt an issue, you don't really need javascript for this: 如果较旧的浏览器不是问题,那么您实际上不需要javascript:

<label>First Name</label>
<td> 
    <input placeholder="Fill" id="firstname" />
    <span>(required)</span>
</td>​

FIDDLE 小提琴

var fill = $('#firstname').val();

$('#firstname').on('focus', function() {
    $(this).val('');
}).on('blur', function() {
    if ($('#firstname').val() == "") {
        $(this).val(fill);
    }
});​

Live example 现场例子

Try: 尝试:

$(".required").on("blur", function(){
  $(this).attr("value", "Fill");
}).on("focus", function(){
  $(this).attr("value", "");
});

Jsfiddle 杰斯菲德尔

<input type="text" id="sample" value="Fill"/>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

You can then use Jquery to handle focus and Blur event for your textbox 然后,您可以使用Jquery处理textbox focusBlur事件

     $('#sample')​.on("focus",function(){
             $(this).val("");
            }).on("blur",function(){
             $(this).val("Fill");
                                       })​;​​

Live Demo 现场演示

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

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