【问题标题】:Javascript / JQuery getting value out of input boxes on tableJavascript / JQuery 从表上的输入框中获取值
【发布时间】:2019-02-05 13:53:05
【问题描述】:

我有一个从 Flask 输入的表格,表格的某些字段有供用户使用的输入框。我试图获得他们输入的价值,我正在努力。我认为这对这里的人们来说是一个简单的问题,但是我已经将头撞在桌子上一段时间了,并且在这里搜索了论坛,却没有弄清楚我做错了什么。

我创建了一个类似但更简单的 HTML 表格,供我们使用。我还无法进入单选按钮部分,因为我仍在尝试解决输入框部分,但我需要同时获得这两个部分。理想情况下,我会将每一行返回到 JSON 数组中。

我包含的 JQuery 返回“无法读取未定义的属性 'GetElementsByTagName'”,但这只是我尝试过的众多示例之一,但均未成功。我已经测试了 .value、.text、.innerHTML 的变体,但我无法获得框内的内容(或单选按钮的值)。

对 JS 新手有什么帮助吗?

//$('.tableRow').each(function() {
//  favoriteBeer = document.getElementsByClassName('favoriteBeer').document.GetElementsByTagName('input');
//  console.log(favoriteBeer);
//});
table {
  border-collapse: collapse;
}

table,
th,
td {
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable">
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>Favorite Food</th>
      <th>Favorite Beer</th>
    </tr>
  </thead>
  <tbody>
    <tr class='tableRow'>
      <td>John</td>
      <td>30</td>
      <td><label><input type="radio" name="favoriteFood1" value="Pizza"/>Pizza</label><label><input type="radio" name="favoriteFood1" value="Tacos" />Tacos</label>
        <td><input type="text" class="favoriteBeer"></td>
    </tr>
    <tr class='tableRow'>
      <td>Joe</td>
      <td>25</td>
      <td><label><input type="radio" name="favoriteFood2" value="Pizza"/>Pizza</label><label><input type="radio" name="favoriteFood2" value="Tacos"/>Tacos</label>
        <td><input type="text" class="favoriteBeer"></td>
    </tr>
    <tr class='tableRow'>
      <td>Sam</td>
      <td>50</td>
      <td><label><input type="radio" name="favoriteFood3" value="Pizza"/>Pizza</label><label><input type="radio" name="favoriteFood3" value="Tacos"/>Tacos</label>
        <td><input type="text" class="favoriteBeer"></td>
    </tr>
  </tbody>
</table>

【问题讨论】:

    标签: javascript jquery html html-table


    【解决方案1】:

    您不能在.getElementsByClassName() 的结果上调用document.getElementsByTagName(),因为.getElementsByClassName() 返回一个“节点列表”并且节点列表没有document 属性。但你可以这样做:

    favoriteBeer = document.getElementsByClassName('favoriteBeer').getElementsByTagName('input');
    

    因为大多数 DOM 查询方法都可以在 document、节点或节点列表上调用。

    但是,.getElementsByClassName().getElementsByTagName() 都返回“实时”节点列表,这意味着每次引用已分配结果的变量时,都必须重新扫描整个文档以确保获得最新的结果。这仅在您动态创建/销毁元素时才有价值。如果您不使用此类代码,则不鼓励使用这些方法,因为它们在性能方面非常浪费。


    现在,由于您使用的是 JQuery,因此您应该始终如一地使用它。只需将有效的 CSS 选择器传递给 JQuery 对象即可扫描 DOM 以查找匹配的元素。

    因此,您可以简单地将类名传递给 JQuery 以获取对您的 DOM 对象的一组引用,然后获取这些 input 元素的 value 属性。但是,您必须等到用户有机会输入一些数据之后才能运行该代码。我在您的代码中添加了一个 button 元素,您可以在准备好查看输入值时单击该元素。

    $("button").on("click", function(){
      // Just passing a valid CSS selector to the JQuery object will
      // return a JQuery "wrapped set" of all matching elements.
      
      // Then, the .each() method takes a function that will automatically
      // be passed the index of the current element being iterated, a DOM reference
      // to the element itself, and a reference to the wrapped set (not used here).
      $('.favoriteBeer').each(function(index, element) {
       // You can use the element argument inside of the .each() loop
       // or you can use the "this" keyword to get the same DOM reference
       console.log(element.value, this.value);
      });
    });
    table {
      border-collapse: collapse;
    }
    
    table,
    th,
    td {
      border: 1px solid black;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table id="myTable">
      <thead>
        <tr>
          <th>Name</th>
          <th>Age</th>
          <th>Favorite Food</th>
          <th>Favorite Beer</th>
        </tr>
      </thead>
      <tbody>
        <tr class='tableRow'>
          <td>John</td>
          <td>30</td>
          <td><label><input type="radio" name="favoriteFood1"/>Pizza</label><label><input type="radio" name="favoriteFood1" />Tacos</label>
            <td><input type="text" class="favoriteBeer"></td>
        </tr>
        <tr class='tableRow'>
          <td>Joe</td>
          <td>25</td>
          <td><label><input type="radio" name="favoriteFood2"/>Pizza</label><label><input type="radio" name="favoriteFood2" />Tacos</label>
            <td><input type="text" class="favoriteBeer"></td>
        </tr>
        <tr class='tableRow'>
          <td>Sam</td>
          <td>50</td>
          <td><label><input type="radio" name="favoriteFood3"/>Pizza</label><label><input type="radio" name="favoriteFood3" />Tacos</label>
            <td><input type="text" class="favoriteBeer"></td>
        </tr>
      </tbody>
    </table>
    <button type="button">Get Data</button>

    【讨论】:

    • 感谢您的精彩解释!既然你这么说,那就很有道理了。
    【解决方案2】:

    你不能运行这一行:

    favoriteBeer = document.getElementsByClassName('favoriteBeer').document.GetElementsByTagName('input');
    

    因为元素 .favoriteBeerdocument 未定义。

    另外,当$('.tableRow').each(function() 运行时,输入字段为空,因为它在页面加载时运行。因此,您可以改为监听 keyup 事件,并在用户每次输入内容时检查输入的当前值。

    像这样:

    $('.favoriteBeer').keyup(function() {
      console.log($(this).val());
    });
    table {
      border-collapse: collapse;
    }
    
    table,
    th,
    td {
      border: 1px solid black;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table id="myTable">
      <thead>
        <tr>
          <th>Name</th>
          <th>Age</th>
          <th>Favorite Food</th>
          <th>Favorite Beer</th>
        </tr>
      </thead>
      <tbody>
        <tr class='tableRow'>
          <td>John</td>
          <td>30</td>
          <td><label><input type="radio" name="favoriteFood1"/>Pizza</label><label><input type="radio" name="favoriteFood1" />Tacos</label>
            <td><input type="text" class="favoriteBeer"></td>
        </tr>
        <tr class='tableRow'>
          <td>Joe</td>
          <td>25</td>
          <td><label><input type="radio" name="favoriteFood2"/>Pizza</label><label><input type="radio" name="favoriteFood2" />Tacos</label>
            <td><input type="text" class="favoriteBeer"></td>
        </tr>
        <tr class='tableRow'>
          <td>Sam</td>
          <td>50</td>
          <td><label><input type="radio" name="favoriteFood3"/>Pizza</label><label><input type="radio" name="favoriteFood3" />Tacos</label>
            <td><input type="text" class="favoriteBeer"></td>
        </tr>
      </tbody>
    </table>

    【讨论】:

    • 谢谢!我很欣赏为什么 $('.tableRow').each(function() 不起作用的解释!这里有很多很好的答案。
    【解决方案3】:

    $(this) 的帮助下使用for 循环来获取每一行的相关值,并使用input:radio:checked 作为选择器获取选定的单选按钮,例如:

    $('button').click(function() {
      $('.tableRow').each(function() {
        var favoriteBeer = $(this).find('.favoriteBeer').val();
        var favoriteFood = $(this).find('input:radio:checked').val();
    
        var dataObj = {
          favoriteBeer: favoriteBeer,
          favoriteFood: favoriteFood
        };
    
        console.log(dataObj);
      });
    })
    table {
      border-collapse: collapse;
    }
    
    table,
    th,
    td {
      border: 1px solid black;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table id="myTable">
      <thead>
        <tr>
          <th>Name</th>
          <th>Age</th>
          <th>Favorite Food</th>
          <th>Favorite Beer</th>
        </tr>
      </thead>
      <tbody>
        <tr class='tableRow'>
          <td>John</td>
          <td>30</td>
          <td><label><input type="radio" name="favoriteFood1" value="Pizza"/>Pizza</label><label><input type="radio" name="favoriteFood1" value="Tacos" />Tacos</label>
            <td><input type="text" class="favoriteBeer"></td>
        </tr>
        <tr class='tableRow'>
          <td>Joe</td>
          <td>25</td>
          <td><label><input type="radio" name="favoriteFood2" value="Pizza"/>Pizza</label><label><input type="radio" name="favoriteFood2" value="Tacos"/>Tacos</label>
            <td><input type="text" class="favoriteBeer"></td>
        </tr>
        <tr class='tableRow'>
          <td>Sam</td>
          <td>50</td>
          <td><label><input type="radio" name="favoriteFood3" value="Pizza"/>Pizza</label><label><input type="radio" name="favoriteFood3" value="Tacos"/>Tacos</label>
            <td><input type="text" class="favoriteBeer"></td>
        </tr>
      </tbody>
    </table>
    <button type="button">Retrieve Data</button>

    【讨论】:

    • 这是一个很好的答案,但对于最喜欢的食物,我只是在每一行都“打开”,而不是在框中实际选择的食物?
    • 如果你能给单选按钮一个值,否则我可以调整脚本以返回标签值?
    • 我刚刚使用无线电值 Pizza/Tacos 进行了更新,如您的更新所示。
    • 效果很好。万一其他人遇到这个问题,你的回答让我成功了 99%。但由于某种原因,在我的真实案例中,而不是我在这里提供的示例中,我需要使用 $(this).find('input:text').val();用于文本框输入。这可能与从 Flask 中提取的输入有关?我不知道为什么,但这是我能想到的唯一区别!非常感谢您和其他所有人的帮助。我得到了这个问题的一些很好的答案。
    • 非常感谢回到这里并将这些信息留给未来的读者,大多数作者在得到正确答案后离开。感谢您的合作和愉快的编码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 2016-01-25
    相关资源
    最近更新 更多