尝试从html

时间:2019-03-22 08:38:29

标签: javascript jquery html

我正在尝试记录将稍后发送到服务器的数据的日志:

  1. 数字数组;
  2. 开奖次数;

一切都在处理数组,但是当我尝试进行console.log绘制时,以下消息显示w.fn.init [input.draw-number, prevObject: w.fn.init(1)]

这是我要从中获取数据的HTML:

             <tr>
                <td><input class="lottery-number" type="number" maxlength="2"></td>
                <td><input class="lottery-number" type="number" maxlength="2"></td>
                <td><input class="lottery-number" type="number" maxlength="2"></td>
                <td><input class="lottery-number" type="number" maxlength="2"></td>
                <td><input class="lottery-number" type="number" maxlength="2"></td>
                <td><input class="lottery-number" type="number" maxlength="2"></td>
                <td><input class="draw-number" type="number" maxlength="6"></td>
                <td><button id="button-send_results" type = "submit">Let's win!</button></td>
            </tr>

我的index.js:

$(document).ready(() => {
  $('#button-send_results').click(() => {
    const userNumbersArray = [];
    const drawNumber = $('.draw-number');
    $('.lottery-number').each(function () {
      userNumbersArray.push($(this).val());
    });

    console.log(userNumbersArray);
    console.log(drawNumber);
  });

});

1 个答案:

答案 0 :(得分:1)

您需要使用.val()来获取输入值,而不是输入DOM元素,例如:

const drawNumber = $('.draw-number').val();

$(document).ready(() => {
  $('#button-send_results').click(() => {
    const userNumbersArray = [];
    const drawNumber = $('.draw-number').val();

    $('.lottery-number').each(function() {
      userNumbersArray.push($(this).val());
    });

    console.log(userNumbersArray);
    console.log(drawNumber);
  });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
  <td><input class="lottery-number" type="number" maxlength="2"></td>
  <td><input class="lottery-number" type="number" maxlength="2"></td>
  <td><input class="lottery-number" type="number" maxlength="2"></td>
  <td><input class="lottery-number" type="number" maxlength="2"></td>
  <td><input class="lottery-number" type="number" maxlength="2"></td>
  <td><input class="lottery-number" type="number" maxlength="2"></td>
  <td><input class="draw-number" type="number" maxlength="6" placeholder="draw-number"></td>
  <td><button id="button-send_results" type="submit">Let's win!</button></td>
</tr>