单击按钮的当前值必须在单击它的ID为“ getir”的文本框中设置。
此操作必须完成,我无法完成Javascript
已更新!
目前,我已经尝试过此代码,并将我的js函数放入该函数中,还设置了按钮onclick="yazdir(yazi)"
function yazdir(yazi) {
++ function Random()....... ++
var yazi = document.getElementById("getir") += yazi;
为了同时获得按钮的值+按钮的文本++,但无法在文本框ID中设置getir
控制台说"yazi is not defined."
function Random() {
return (Math.floor(Math.random() * 10));
}
function randomValue() {
var i;
for (i = 0; i < 10; i++) {
a = Random(); //a = 5
var x = document.getElementById("s" + i); // 1 değeri için x=1
var y = document.getElementById("s" + a);
var temp;
temp = x.value; // x : 0
x.value = y.value; // y :5
y.value = temp; //temp :0
}
}
<table>
<tr>
<input type="text" id="getir" value="" name="getir">
</tr>
<br>
<tr>
<input id="s1" type="button" class="numbers btn btn-outline-success mt-3" onclick="randomValue()"
value="1">
<input id="s2" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="randomValue()"
value="2">
<input id="s3" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="randomValue()"
value="3">
</tr>
<br>
<tr>
<input id="s4" type="button" class="numbers btn btn-outline-success mt-3" onclick="randomValue()"
value="4">
<input id="s5" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="randomValue()"
value="5">
<input id="s6" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="randomValue()"
value="6">
</tr>
<br>
<tr>
<input id="s7" type="button" class="numbers btn btn-outline-success mt-3" onclick="randomValue()"
value="7">
<input id="s9" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="randomValue()"
value="8">
<input id="s8" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="randomValue()"
value="9">
</tr>
<br>
<tr>
<input id="s0" type="button" class="numbers btn btn-outline-success mt-3" onclick="randomValue()"
value="0">
<input id="btnSil" type="button" class="btn btn-outline-danger mt-3 ml-5" onclick="silYazi()"
value="Sil">
</tr>
</table>
数字是随机的,这很好。我需要更改它,因为单击按钮时不应重复0-9数字
从随机分布中选择数字是行不通的,因为每次抽签都是独立的,并且人口非常少,因此您会得到重复。
而不是选择随机数,您想要的是shuffle一个固定的数字列表(此处为0到9)。这是应用于键盘(使用this Fisher-Yates implementation)的快速概念验证:
const buttons = document.querySelectorAll("input");
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (let i = 0, len = buttons.length; i < len; i++) {
buttons[i].value = numbers[i];
buttons[i].addEventListener("click", shuffleKeypad);
}
function shuffleKeypad() {
shuffle(numbers);
for (let i = 0, len = buttons.length; i < len; i++) {
buttons[i].value = numbers[i];
}
}
/**
* Shuffles array in place.
* @param {Array} a items An array containing the items.
*/
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
<table>
<tbody>
<tr>
<td><input type="button"></td>
<td><input type="button"></td>
<td><input type="button"></td>
</tr>
<tr>
<td><input type="button"></td>
<td><input type="button"></td>
<td><input type="button"></td>
</tr>
<tr>
<td><input type="button"></td>
<td><input type="button"></td>
<td><input type="button"></td>
</tr>
<tr>
<td><input type="button"></td>
</tr>
</tbody>
</table>