这是我的HTML:
<input name="code" type="text" id="box" /><input id = "button" type="button" value="Create Random String"><br />
这是我用于生成字符串的jquery:
var o = { // Define Letter Values
1,2,3,4,5,6,7,8,9,0
};
$('#button').on("click", function(){
var chars = "123456789";
var string_length = 8;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
//document.randform.code.value = randomstring;
//document.code.value = randomstring;
$('#box').val(randomstring);
$( "#value" ).val(function() {
var str = $(this).val();
var arr = split('');
var total = 0;
$.each(arr, function(i, v){
var letterIndex = o[v];
if(i === 0){
total = letterIndex;
} else {
total = total * letterIndex;
}
});
问题是,如何在#box
文本框中生成字符串并同时将#value
文本框中生成的数字相乘?
答案 0 :(得分:1)
也许您可以应用此代码段中的代码。它
var result = document.querySelector('pre');
var n = 10;
while (n--) {
var r1 = randomNumber(10);
var r2 = randomNumber(10);
var r3 = r1 * r2;
result.textContent += '\n' + r1+' * '+r2 + ' = ' + r3;
}
function randomNumber(len) {
var digit = Math.floor(1 + Math.random() * 9);
var n1 = (Math.pow(10, len-1)) * digit;
var n2 = n1/10;
return Math.floor(n1+Math.random() * n2 - 1);
}
<pre>testing numbers</pre>