单击js中的按钮时如何生成随机字母

时间:2016-11-14 10:39:08

标签: javascript html

当我点击它时,如何制作带有随机字母的js按钮。

3 个答案:

答案 0 :(得分:2)

使用Math.random方法和String.fromCharCode方法。

console.log(
  String.fromCharCode(
    Math.floor(Math.random() * 26) + 97
  )
)

答案 1 :(得分:1)

试试这段代码:

document.getElementById("rndletter").addEventListener("click", function() {
  var result = String.fromCharCode(Math.floor(Math.random() * (122 - 97)) + 97);
  
  document.getElementById("result").innerText = result;
});
<button id="rndletter">Generate random letter</button>
<div id="result"></div>

答案 2 :(得分:0)

试试这个:

HTML文件

  $rows = array ('packaging' => array ('0'=> 1,'1' => 2),'quantity' => array('0'=> 1,'1' => 2),'total-weight' => array ('0'=> 1,'1' => 2),
'length' =>array ('0'=> 1,'1' => 2)

 );
  $res_array = array();
  $total_records = count($rows['packaging']);
 for($i=0;$i<$total_records;$i++)
 {
     $res_array[] = array('packaging'=>$rows['packaging']   [$i],'quantity'=>$rows['quantity'][$i],
    'total-weight'=>$rows['total-weight'][$i],'length'=>$rows['length'] [$i]);
 }
print_r($res_array);

Javascript文件

<form name="randform">
<input type="button" value="Create Random String" onClick="randomString();">&nbsp;
<input type="text" name="randomfield" value="">
</form>