简体   繁体   English

在TR表中获取输入元素值

[英]Getting an input element value inside TR table

I have an html table called #DimensionAttributes whose columns are as follows: 我有一个名为#DimensionAttributes的html表,其列如下:

<td><input type="text" name="txtLEName" value="@attribute.Key" /></td>

I intentionally wanted to maintain the name of the input element static. 我有意要保持输入元素名称的静态。 There can be multiple rows bound to this table. 可以有多个行绑定到该表。 How do I loop through each row and find out the input elements value using the ID. 如何遍历每一行并使用ID找出输入元素的值。 The reason I wanted to use ID is because there can be multiple columns that would come soon in this table. 我想使用ID的原因是因为此表中很快就会有多个列。 Please help 请帮忙

使用选择器,也许:

$("input[name='txtLEName']")

Well, first all, an ID must be unique, instead used class. 好吧,首先,一个ID必须是唯一的,而不是使用的类。 Or if you eagerly wanted to use ID, try selecting by this : 或者,如果您渴望使用ID,请尝试通过以下方式进行选择:

Suppose you have this two element with same ID name : 假设您有两个具有相同ID名称的元素:

<td><input type="text" name="txtLEName" value="@attribute.Key" id="hello"/></td>
<td><input type="text" name="txtLEName" value="@attribute.Key" id="hello"/></td>

Then your selector should be : 那么您的选择器应为:

$('[id="hello"]') 

Or another way, if you want to increment the ID name like : 或者,如果您想增加ID名称,例如:

<td><input type="text" name="txtLEName" value="@attribute.Key" id="hello1"/></td>
<td><input type="text" name="txtLEName" value="@attribute.Key" id="hello2"/></td>

As you dont know how many of element since the element created dynamically, then you can select it using this : 由于您不知道自动态创建元素以来有多少个元素,因此可以使用以下命令进行选择:

 $('[id^="hello"]') // find the element ID name start with `hello`

If you want to loop thru each row, why not write something like this: 如果要遍历每一行,为什么不写这样的东西:

$("#DimensionAttributes tr input[name='txtLEName']").each(function(){ 
    //Your code here:
    //$(this).val()
});
var $rows = $('#table').find("tr:not(:eq(0))");
$rows.each(function () {
    var $tds = $(this).find('td');
    var val = $tds.find(':text').val();
    console.log(val);



})

Assuming you have 1 input text each row you can select the table id find the tr and iterate and find td with input and get the value 假设每行有1个输入文本,则可以选择表id来找到tr并使用输入迭代td并获取值

DEMO 演示

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

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