简体   繁体   English

jQuery随机输入占位符

[英]JQuery random input placeholder

How can i set random placeholder text from array of strings in jquery after document is loaded? 加载文档后,如何在jquery中的字符串数组中设置随机占位符文本? Now i have just "hardcoded" the setting for placeholder? 现在,我刚刚“硬编码”了占位符的设置? But how to do it dynamically? 但是如何动态地进行呢?

jQuery(function($) {
   $(document).ready(function () {
    var list = new Array();
    list.push("1");
    list.push("2");
        $('#search').attr("placeholder","placeholder text");
    });
});

Just how one time see 1, other 2...? 一次如何看到1,其他2 ...?

$(function(){
  var list = [...],
      r = Math.floor(Math.random() * list.length);
  $('#search').prop('placeholder',list[r]);
});

that would use any of the possibilities within list and assign the placeholder a new random value from it. 它将使用list任何可能性,并从中为占位符分配一个新的随机值。

Example

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

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