我必须在香草JavaScript中将Button的值设置为Textbox,而不是jQuery

问题描述 投票:0回答:1

我必须在JavaScript中将按钮的值设置为文本框

单击按钮时,必须在id为“ getir”的文本框中设置按钮的当前值。

这必须用JavaScript完成,但我做不到

HTML表格

<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>

JavaScript(Randomizer运行正常-只需将值从按钮设置为文本框)

////////////////////////////////////////////////////////


function Random() {
    return (Math.floor(Math.random() * 10));
}

function randomValue() {
    var x = document.getElementsByClassName("numbers")
    var i;
    var temp;
    a = Random();

    for (i = 0; i < x.length; i++) {
        temp = x[i].value;
        x[i].value = x[a].value;
        x[a].value = temp;
    }
}

function textB() {
    x = document.getElementById('getir').value;
}


/////////////////////////////////////////////////////////////////////

function silYazi() {
    document.getElementById("getir").value = "";
}
javascript html dom
1个回答
0
投票

不是选择随机数,而是要shuffle固定的数字列表。

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>
© www.soinside.com 2019 - 2024. All rights reserved.