Home >Web Front-end >JS Tutorial >How to set and remove the default value of text box in jQuery_jquery

How to set and remove the default value of text box in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 16:10:461291browse

The example in this article describes how to set and remove the default value of a text box with jQuery. Share it with everyone for your reference. The specific analysis is as follows:

Initially, the text box is set to a default value. When the cursor moves to the text box, if the current value of the text box is the default value, then it is cleared; when leaving the text box, if the text box value is empty, then the text box value is set to the default value.

The code is as follows:

$(document).ready(function() {
  //each遍历文本框
  $(".input").each(function() {
    //保存当前文本框的值
    var vdefault = this.value;
    $(this).focus(function() {
      //获得焦点时,如果值为默认值,则设置为空
      if (this.value == vdefault) {
        this.value = "";
      }
    });
    $(this).blur(function() {
      //失去焦点时,如果值为空,则设置为默认值
      if (this.value == "") {
        this.value = vdefault;
      }
    });
  });
});

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn