我必须在JS中将Button的值设置为Textbox — NOT JQUERY

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

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

单击按钮的当前值必须在单击它的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>

[JS UPDATED Randomizer可以正常工作-只需将按钮的值设置为TEXTBOX即可!

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


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.