简体   繁体   English

输入字段的多个默认值

[英]multiple default values for input field

I would like to have a search box input field that flashes multiple default values when not in focus. 我希望有一个搜索框输入字段,当焦点不对准时会闪烁多个默认值。 As in "New York" for 3 seconds, then "Los Angeles" for 3 seconds, etc. or to simulate typing of these would also be nice. 如在“纽约”中停留3秒钟,然后在“洛杉矶”中停留3秒钟,依此类推,或者模拟它们的键入也将是不错的。

What i have done is this 我所做的是这个

http://jsfiddle.net/5aD7W/1/ http://jsfiddle.net/5aD7W/1/

but the problem is that the .val() gets overridden every 3 seconds even when it is in focus. 但是问题是.val()每隔3秒就会被覆盖,即使它处于焦点位置也是如此。

One way That I came up with: 我想到的一种方法:

var words = ['New York', 'Los Angeles', 'Home'];
var $search = $('#searchId');
var index = -1;

function foo() {

    if ($search.is(':focus')) {
        return;
    }
    index++;
    $search.val(words[index % words.length]);
}​

setInterval(foo, 3000);

Live DEMO 现场演示

You can do it like this Demo on JsFiddle 您可以像在JsFiddle上的演示一样进行操作

In html 在html中

<input id="txtCity" type="text" value="New York"  />​

IN script IN脚本

  arr = ["New York", "Los Angesles", "London"];
txtCity = $('#txtCity');
i=0;
setInterval(function(){ 

    if (txtCity.is(':focus')) 
        return;   
    txtCity.val(arr[i++]);
    if(i==arr.length)
         i = 0;
}, 2000);

$('#txtCity').focus(function(){
    this.value=""
 });
​
​

You will need some scripting to do this, so jQuery is a good way to go. 您将需要一些脚本来执行此操作,因此jQuery是一个不错的选择。 Simple markup won't solve your problem. 简单的标记无法解决您的问题。 Related: Changing the "placeholder" attribute of HTML5 input elements dynamically using Javascript 相关: 使用JavaScript动态更改HTML5输入元素的“占位符”属性

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

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