有必要以某种方式解决将值从输入传递到这一行中的参数的inputCharCode1问题inputCharCode2String.fromCharCode(getRandomIntInclusive(inputCharCode1, inputCharCode2));
我不明白它是如何工作的以及如何设置值。
var input = document.querySelector('.input');
var inputLength = document.querySelector('.inputLength');
var btn = document.querySelector('.button');
let arr = [];
btn.addEventListener('click', function() {
var inputCharCode1 = document.querySelector('.inputCharCode1');
var inputCharCode2 = document.querySelector('.inputCharCode2');
arr = [];
for (var i = 0; i < (Number(inputLength.value)); i++) {
arr[i] = String.fromCharCode(getRandomIntInclusive(inputCharCode2, inputCharCode1));
}
console.log(arr);
input.value = arr.join('');
});
function getRandomIntInclusive(max, min) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
<input class="input" type="text">
<br><br>
<label for="inputLength">String Length: <input class="inputLength" name="inputLength" type="text"></label>
<br><br>
<label for="inputCharCode1">From: <input type="text" name="inputCharCode1" class="inputCharCode1"></label>
<label for="inputCharCode2">To: <input type="text" name="inputCharCode2" class="inputCharCode2"></label>
<br><br>
<button class="button">Запустить</button>
document.querySelector返回一个 HTMLElement,该函数
getRandomIntInclusive接受两个数字。排队
元素被传递给函数而不是数字。由于要获取的元素是输入的,因此您可以使用
value.在某些情况下,这就足够了,因为可以在函数内部使用运算符,这些函数本身将其操作数转换为数字,但在这种情况下,最好在将参数传递给函数之前将其转换为数字。
例如,使用函数
Number