我无法从其他输入文本值中获取随机文本。
当用户写名称
时<input type="text" name="txtuser" onchange="getalias(this)">
并且用户以语法形式获取别名
<input id="alias" type="text" name="txtalias">
onchange代码
<script>
function getalias() {
...
}
</script>
,结果就像这样
name : riski
alias: rki_ (or other)
任何人都可以举例说明如何从&#34; txtuser&#34;中随机取值并以别名输入文本显示。
答案 0 :(得分:0)
试试这个:
function shuffle(array) {
var currentIndex = array.length,
temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function getalias(that) {
var val = that.value;
var alias = '';
var splitVal = val.split('');
var shuffled = shuffle(splitVal);
for (var i = 0, len = shuffled.length; i < len; i++) {
alias += shuffled[i];
}
document.getElementById('alias').value = alias;
}
<input type="text" name="txtuser" onkeyup="getalias(this)">
<input id="alias" type="text" name="txtalias">