简体   繁体   English

Javascript / JQuery从表上的输入框中获取值

[英]Javascript / JQuery getting value out of input boxes on table

I have a table with input from Flask, and some fields of the table have input boxes for the user. 我有一个表格,其中包含来自Flask的输入,表格的某些字段包含用户的输入框。 I am trying to get the value of what they input and I am struggling. 我试图获得他们输入的价值,我正在努力。 I figure this is an easy question for people here, but I have been banging my head against my desk for a while now and searched the forums here without figuring out what I'm doing wrong. 我认为这对于这里的人来说是一个简单的问题,但我现在已经在我的桌子上敲了一会儿,并在这里搜索论坛,却没有弄清楚我做错了什么。

I created a similar but much simpler HTML table to what I have for us to play around with. 我创建了一个类似但更简单的HTML表格,以供我们使用。 I have not been able to get to the radio button part yet because I'm still trying to get the input box part solved, but I will be needing to get both of those. 我还没有能够进入单选按钮部分,因为我仍然试图让输入框部分解决,但我需要得到这两个部分。 Ideally, I'd be returning each row into a JSON array. 理想情况下,我会将每一行返回到JSON数组中。

The JQuery I included returns "Cannot read property 'GetElementsByTagName' of undefined", but it's only one of the many examples I've tried without any success. 我包含的JQuery返回“无法读取未定义的属性'GetElementsByTagName'”,但它只是我尝试过的许多示例中的一个没有任何成功。 I've tested variations with .value, .text, .innerHTML, but I just can't get what's inside the box (or the radio button value for that matter). 我已经使用.value,.text,.innerHTML测试了变体,但我无法得到框内的内容(或者单元格的单选按钮值)。

Any help for a JS novice? 对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> 

You can't call document.getElementsByTagName() on the results of .getElementsByClassName() because .getElementsByClassName() returns a "node list" and a node list doesn't have a document property. 您不能在.getElementsByClassName()的结果上调用document.getElementsByTagName() ,因为.getElementsByClassName()返回“节点列表”,而节点列表没有document属性。 But you could do this: 但你可以这样做:

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

Because most DOM query methods can be called on document , a node, or a node list. 因为大多数DOM查询方法可以在document ,节点或节点列表上调用。

However, .getElementsByClassName() and .getElementsByTagName() both return "live" node lists, which means that every time you reference the variable that you've assigned the results to, the entire document must be re-scanned to ensure you get the most up-to-date results. 但是, .getElementsByClassName().getElementsByTagName()都返回“实时”节点列表,这意味着每次引用已分配结果的变量时,必须重新扫描整个文档以确保获得最新的结果。 This is only valuable when you have elements being created/destroyed dynamically. 只有在动态创建/销毁元素时,这才有价值。 If you aren't working with that kind of code, the use of these methods is discouraged because they are very wasteful, in terms of performance. 如果您不使用这种代码,则不鼓励使用这些方法,因为它们在性能方面非常浪费。


Now, since you are using JQuery, you should use it consistently. 现在,既然您正在使用JQuery,那么您应该始终如一地使用它。 Just pass a valid CSS selector into the JQuery object to scan the DOM for matching elements. 只需将有效的CSS选择器传递给JQuery对象,即可扫描DOM以查找匹配的元素。

So, you can simply pass the class name into JQuery to obtain a set of references to your DOM objects and then get the value property of those input elements. 因此,您只需将类名传递给JQuery即可获得对DOM对象的一组引用,然后获取这些input元素的value属性。 But, you do have to wait to run that code until after the user has had a chance to enter some data. 但是,在用户有机会输入某些数据之前,您必须等待运行该代码。 I've added a button element to your code which you can click when you are ready to see the input values. 我已经为您的代码添加了一个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> 

Use a for loop with the help of $(this) to get the related values of every row, and get the selected radio buttons using input:radio:checked as selector like : $(this)的帮助下使用for循环来获取每一行的相关值,并使用input:radio:checked获取所选单选按钮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> 

You cannot run this line: 你不能运行这一行:

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

Because document of element .favoriteBeer is undefined. 因为元素.favoriteBeer document未定义。

Also, when $('.tableRow').each(function() runs, the input field is empty, as it runs as the page loaded. So what you can do instead is listen on the keyup event and check the current value of input every time the user types something. 此外,当$('.tableRow').each(function()运行时,输入字段为空,因为它在页面加载时运行。所以你可以做的是监听keyup事件并检查当前值每次用户输入内容时输入。

Like so: 像这样:

 $('.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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM