简体   繁体   English

如何使用 jQuery 获得选定的行?

[英]How can I get selected row using jQuery?

This is my table row within the tbody of my table.这是我表格中的tbody行。 I get dropdown value, text value but I can't get selected row value.我得到下拉值,文本值,但我无法获得选定的行值。 My row value returns "undefined".我的行值返回“未定义”。

 $(document).on("click", "#skorKartDegerle", function() { $("#modal_evaluation").modal("show"); }); $(document).on("click", "#btnKaydet", function() { var arrList = []; var isEmptyAnswer = false; $("#evaluationTable > tbody > tr").each(function() { var line = $(this).find(".answerLine").val(); var ddlVal = $(this).find(".answerddl").val(); var txtVal = $(this).find(".answertxt").val(); var obj = '{"line":"' + line + '","ddlVal":"' + ddlVal + '","txtVal":"' + txtVal + '"}'; arrList.push(obj); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tbody> <tr class="answerLine" value="2"> <td>FR002</td> <td>Koton Mağazacılık</td> <td>1800</td> <td>A</td> <td>Kabul</td> <td class="select" value="0"> <select class="answerddl"> <option value="1">Kabul</option> <option value="2">Ret</option> </select> </td> <td><input type="text" class="answertxt"></td> </tr> </tbody> </table>

Part of the issue is because tr elements do not have a value attribute.部分问题是因为tr元素没有value属性。 To do what you require you could use a data attribute instead, to store custom metadata on the element.要执行您需要的操作,您可以改用data属性,将自定义元数据存储在元素上。 The other part is that this is a reference to the tr element.另一部分是this是对tr元素的引用。 You're then calling find() on the element you're looking to target, so it will not be found as that method looks for descendants only.然后,您在要定位的元素上调用find() ,因此不会找到它,因为该方法仅查找后代。

In addition it's worth noting that you can make the logic more succinct by using map() to build the array instead of explicitly looping with each() and also that it would be better practice to store objects in the array and only JSON encode it before transferring via AJAX.此外值得注意的是,您可以通过使用map()构建数组而不是使用each()显式循环来使逻辑更简洁,而且最好将对象存储在数组中,并且之前只有 JSON 对其进行编码通过 AJAX 传输。

 $(document).on("click", "#btnKaydet", function() { var isEmptyAnswer = false; let arrList = $("#evaluationTable > tbody > tr").map((i, tr) => { let $tr = $(tr); return { line: $tr.data('value'), ddlVal: $tr.find(".answerddl").val(), txtVal: $tr.find(".answertxt").val() } }).get(); console.log(arrList); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table id="evaluationTable"> <tbody> <tr class="answerLine" data-value="2"> <td>FR002</td> <td>Koton Mağazacılık</td> <td>1800</td> <td>A</td> <td>Kabul</td> <td class="select" value="0"> <select class="answerddl"> <option value="1">Kabul</option> <option value="2">Ret</option> </select> </td> <td><input type="text" class="answertxt"></td> </tr> </tbody> </table> <button id="btnKaydet">Click me</button>

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

相关问题 如何使用 jQuery 在 Kendo Grid 中获取选定的行 - How can I get the selected row in Kendo Grid using jQuery 使用Javascript / jquery如何从所选行的每个单元格中获取值 - Using Javascript/jquery how can I get values from each cells in a selected row 如何使用 Javascript 或 Jquery 获取所选选项的值? - How can i get the value of the selected option using Javascript or Jquery? 如何显示表格行取决于使用jquery匹配选定的下拉框值 - How can i display table row depends on matching selected dropdow box values using jquery 如何在Javascript或JQuery中获取Gridview中的选定行 - How do I get the selected row in Gridview in Javascript or JQuery 在jQuery中,如何获取使用复选框选择的表的特定行的值? - In jquery how to get the values of a particular row of a table that is selected using checkbox? 如何使用jquery获取下一页上的选定行值? - How to get selected row value on the next page using jquery? 如何通过使用jQuery从WebGrid的选定行中获取值 - How to get the value from a selected row of a webgrid by using jquery 使用服务器绑定时,如何从Kendo网格选择的行中获取数据? - How can I get the data from a Kendo grid selected row when using server binding? 如何在剑道中获取选定的行数据 - How can I get selected row data in kendo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM