在javascript / jquery中的uniqid()? [英] uniqid() in javascript/jquery?

查看:311
本文介绍了在javascript / jquery中的uniqid()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

javascript中这个函数的等价物是什么:

what's the equivalent of this function in javascript:

http://php.net/manual/en/function.uniqid.php

基本上我需要生成一个随机ID看起来像这样: a4245f54345 并以字母字符开头(所以我可以将它用作CSS id)

Basically I need to generate a random ID that looks like: a4245f54345 and starts with a alphabetic character (so I can use it as a CSS id)

推荐答案

试试这个(在php中工作)。

Try this (Work in php).

$prefix = chr(rand(97,121));  
$uniqid =  $prefix.uniqid(); // $uniqid = uniqid($prefix);

尝试使用JavaScript ::

Try this for JavaScript::

var n = Math.floor(Math.random() * 11);
var k = Math.floor(Math.random() * 1000000);
var m = String.fromCharCode(n) + k;

这篇关于在javascript / jquery中的uniqid()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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