当我点击它时,如何制作带有随机字母的js按钮。
答案 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();">
<input type="text" name="randomfield" value="">
</form>