简体   繁体   English

在 JQuery 中将输入值设置为属性值

[英]Setting input value to attribute value in JQuery

I have a number of <input /> boxes which I want to start off having a value of something like " Enter your name... ".我有许多<input />框,我想开始时它们的值类似于“输入您的名字... ”。

When you focus them, the value becomes empty and you can type away.当您聚焦它们时,该值变为空,您可以输入。 When you blur them, if nothing has been entered, then it goes back to " Enter your name... ".当您模糊它们时,如果未输入任何内容,则返回“输入您的姓名... ”。

I thought of having something like this:我想过这样的事情:

<input id="name" _startValue="Enter your name..." />

Then, something like this:然后,像这样:

$(document).ready($("input").val($(this).attr(_startValue)));

This initially should set the value to _startValue but it does nothing.这最初应该将value设置为_startValue但它什么也不做。 Replacing the line with:用以下内容替换该行:

$(document).ready($("input").val("hello"));

does work, however, so the problem must be with the $(this) or the attr() .但是,确实有效,因此问题必须出在$(this)attr()

First of all, how do I get this to work.首先,我如何让这个工作。 Secondly, if I am trying to do this in a really retarded way, what is a good way to get this functionality?其次,如果我试图以一种非常迟钝的方式做到这一点,那么获得此功能的好方法是什么?

我相信最好使用如下占位符:

 <input id="name" placeholder="Enter your name..." />
                // v---you're not passing a function
 $(document).ready($("input").val($(this).attr(_startValue)));
          // `this` isn't magic-------^---- It doesn't just mean what you want

Should be more like this:应该更像这样:

$(document).ready(function() {
    $("input").val(function() {
        return $(this).attr("_startValue");
    });
});

There are already libraries for this, and if you are already using jquery you should use them.已经有用于此的库,如果您已经在使用 jquery,则应该使用它们。

https://github.com/mathiasbynens/jquery-placeholder https://github.com/mathiasbynens/jquery-placeholder

just add the attribute "placeholder" and invoque the function:只需添加属性“占位符”并调用函数:

<input placeholder="my placeholder">
<script type="text/javascript">
    $("document").ready(function(){
        $("input").placeholder();
    });
</script>

Note that you only need to add the plugin if you need old browser support (in IE specially), otherwise, the attribute is enough.注意只有需要旧浏览器支持时才需要添加插件(特别是IE浏览器),否则这个属性就够了。

Also, consider that if you code this, it will take you errors like submitting the default value of the form.另外,请考虑如果您对此进行编码,则会出现诸如提交表单默认值之类的错误。 What jquery plugins do generally is to make a <span> or whatever and place it on top of the input when the input is empty, and hide it when the input is not empty. jquery 插件通常做的是制作一个<span>什么的,当输入为空时将它放在输入的顶部,并在输入不为空时隐藏它。

A common way to mimic placeholders is to put the placeholder text in the element's value, then check the value on focus like:模仿占位符的常用方法是将占位符文本放在元素的值中,然后检查焦点上的值,例如:

if (this.value == this.defaultValue) this.value = '';

and then on blur:然后模糊:

if (this.value == '') this.value = this.defaultValue;

Please don't use placeholders instead of labels or onscreen help (eg format for dates).请不要使用占位符代替标签或屏幕帮助(例如日期格式)。 If a browser doesn't support the placeholder attribute, it's probably best not to emulate them if using it for the default value is an issue.如果浏览器不支持占位符属性,如果使用它作为默认值是一个问题,最好不要模拟它们。

After all, placeholders are a "nice to have", they should not be fundamental to using the form correctly.毕竟,占位符是一个“很好的东西”,它们不应该是正确使用表单的基础。

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

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