简体   繁体   English

如何在变量中保存INPUT的值?

[英]How to save the value of INPUT in variable?

How to save the value of INPUT in variable to not to write a lot of duplicate code? 如何在变量中保存INPUT的值以免编写大量重复的代码?

like var input = $(this).val(); 像var input = $(this).val();

full example 完整的例子

<div id="form">
    1. <input type="text" value="title" />
    2. <input type="text" value="value" />
</div>

$(function(){
  $('#form input:eq(0)').bind({
    focus: function(){
       if($(this).val()=='title'){
            $(this).val('');
        }
     },
     blur: function(){
       if($(this).val() == ''){
          $(this).val('title');
        }
     }
  });

  $('#form input:eq(1)').bind({
    focus: function(){
      if($(this).val()=='value'){
         $(this).val('');
      }
    },
     blur: function(){
        if($(this).val() == ''){
           $(this).val('value');
        }
    }
  });
});

I'm not exactly sure what you are asking, but this refactoring will work for toggling the value. 我不确定您要问的是什么,但是此重构可用于切换值。 EDIT: added default attribute to the html elements and shortened jQuery (still readable though) http://jsfiddle.net/UmZeZ/ 编辑:向html元素添加了默认属性,并缩短了jQuery(尽管仍可读) http://jsfiddle.net/UmZeZ/

<div id="form">
    1. <input type="text" value="title" default="title" />
    2. <input type="text" value="value" default="value" />
</div>


$(function() {
    $('#form input').bind('focus blur', function() {
        var value = $(this).attr('default');
        if ($(this).attr('value') == value) {
            $(this).attr('value', '');
        } else if ($(this).attr('value') === '') {
            $(this).attr('value', value);
        }
    });
});

To accomplish what you want, I would suggest using the HTML5 placeholder attribute. 为了完成您想要的,我建议使用HTML5 placeholder属性。 With Modernizr, we can detect browser support for this feature, and with this simple piece of code, we can get it to work even for browsers that do not support placeholder . 使用Modernizr,我们可以检测到浏览器对此功能的支持,并且通过这段简单的代码,即使对于不支持placeholder浏览器,我们也可以使其正常工作。

if(!Modernizr.input.placeholder){
    var input = $('input[type="text"]');

    input.focus(function(){
        if(this.value === this.getAttribute('placeHolder')) this.value = '';
    }).blur(function(){
        if(this.value === '') this.value = this.getAttribute('placeHolder');
    }).blur();
}

See a live demo of this here: http://www.jsfiddle.net/yijiang/cTDsL/1 在此处观看此演示的现场演示: http : //www.jsfiddle.net/yijiang/cTDsL/1

Here is my solution. 这是我的解决方案。 I would work to any field which has class="set-default" 我会在任何具有class =“ set-default”的字段上工作

Checkout the working example 检出工作示例

Here is the code: 这是代码:

$(function(){
    $('.set-default').bind({
    focus: function(){
        if(typeof($(this).data('def')) == 'undefined'){
               $(this).data('def', this.value)
        }
        if(this.value == $(this).data('def')){
           this.value = '';
        }
     },
     blur: function(){
       if(this.value == ''){
          this.value = $(this).data('def');
       }
     }
    })
});

basically all fields which had the class set-default will act as you like. 基本上所有具有class set-default的字段都将按照您的意愿运行。 You can always change the selector to $('#form input') but I think it's not useful. 您可以始终将选择器更改为$('#form input'),但我认为这没有用。

HTH 高温超导

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

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