如何定位所有输入文本和密码值? [英] How to target all input text and password value?

查看:65
本文介绍了如何定位所有输入文本和密码值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何定位页面文档中所有表单上的每个输入(文本和密码)字段,以便在使用此单个脚本进行焦点时删除默认值:

I'm trying to figure out how to target every input(text and password) field on all forms on the page document to remove the default value when on focus with this single script:

$(document).ready(function()
{
    Input = $('input');
    default_value = Input.val();

    $('input').focus(function() 
    {
        if($(this).val() == default_value)
        {
            $(this).val("");
        }
    }).blur(function()
    {
        if($(this).val().length == 0)
        {
            $(this).val(default_value);
        }
    });
});

它仅适用于我页面上第一个表单中的第一个输入文本元素,但没有休息。请帮忙!

It works only on the first input text element in the first form on my page, but nothing on the rest. Please help!

推荐答案

那是因为 val 只返回值首先选择的元素,而不是存储值,您可以使用 defaultValue 属性/ DOM / HTMLInputElementrel =nofollow> HTMLInputElement object。

That's because val only returns the value of the first selected element, instead of storing the values, you can use defaultValue property of the HTMLInputElement object.

$('input[type=text], input[type=password]').focus(function() {
   if (this.value === this.defaultValue) $(this).val("");
}).blur(function(){
   if (this.value.length === 0) this.value = this.defaultValue;
});

http://jsfiddle.net/MscgZ/

这篇关于如何定位所有输入文本和密码值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆