从html表中的特定列获取值

时间:2021-09-23 08:03:49

I have a controller setup and the view contains a table with some information. When the submit button is pressed on the jsp page, I would like to get the information from the 2nd column in the table so that I can process that information in the controller. The rows in the table can vary from 2 to 100, so I was looking for something that would get the value from the 2nd column irrespective of the number of rows. I want to extract the information from td class urltext. I did my research but I was not able to find any information related to this so any ideas would be highly appreciated. Thanks.

我有一个控制器设置,视图包含一个包含一些信息的表。当在jsp页面上按下提交按钮时,我想从表中的第二列获取信息,以便我可以在控制器中处理该信息。表中的行可以在2到100之间变化,因此我正在寻找能够从第2列获取值的内容,而不管行数。我想从td类urltext中提取信息。我做了我的研究,但我无法找到任何与此相关的信息,因此任何想法都将受到高度赞赏。谢谢。

<table name="urlTable" id="urlList"  style="width:100%; overflow: scroll;">
<tr>
    <th>Select</th>
    <th>URL</th>
</tr>
<c:forEach var="urlList" items="${command.urlList}">
<tr>
    <td><input type="checkbox" name="chk"/></td>
    <td class="urlText"><input class="urlValue" type="text" value="${urlList}" style="font-size: 13px; border: none;" size="85px;" readonly="readonly"/></td>
</tr>
</c:forEach>
</table>                                

2 个解决方案

#1


0  

Let's start by using thead and tbody elements as we ought.

让我们从我们应该使用thead和tbody元素开始。

<table name="urlTable" id="urlList"  style="width:100%; overflow: scroll;">

    <thead>
        <tr>
            <th>Select</th>
            <th>URL</th>
        </tr>
    </thead>
    <tbody>
        <c:forEach var="urlList" items="${command.urlList}">
        <tr>
            <td><input type="checkbox" name="chk"/></td>
            <td class="urlText"><input class="urlValue" type="text" value="${urlList}" style="font-size: 13px; border: none;" size="85px;" readonly="readonly"/></td>
        </tr>
        </c:forEach>
    </tbody>

</table> 

Then just target the second column of each row in the tbody. I assume you're targeting the input.

然后只需定位tbody中每行的第二列。我假设你的目标是输入。

var inputs = $("#urlList > tbody > tr > td:nth-child(2) > input");

I don't know what you want to do with the input at this point. If you want an Array of the values, do this.

我不知道你现在想要做什么输入。如果需要值的数组,请执行此操作。

var vals = inputs.map(function() { 
                         return this.value; 
                      }).toArray();

#2


0  

you can try this:

你可以试试这个:

var values = [];
$('#urlList .urlvalue').each(function(){
    var val = $(this).val();
    values.push(val);
});

The array values have all input values from your table.

数组值包含表中的所有输入值。

#1


0  

Let's start by using thead and tbody elements as we ought.

让我们从我们应该使用thead和tbody元素开始。

<table name="urlTable" id="urlList"  style="width:100%; overflow: scroll;">

    <thead>
        <tr>
            <th>Select</th>
            <th>URL</th>
        </tr>
    </thead>
    <tbody>
        <c:forEach var="urlList" items="${command.urlList}">
        <tr>
            <td><input type="checkbox" name="chk"/></td>
            <td class="urlText"><input class="urlValue" type="text" value="${urlList}" style="font-size: 13px; border: none;" size="85px;" readonly="readonly"/></td>
        </tr>
        </c:forEach>
    </tbody>

</table> 

Then just target the second column of each row in the tbody. I assume you're targeting the input.

然后只需定位tbody中每行的第二列。我假设你的目标是输入。

var inputs = $("#urlList > tbody > tr > td:nth-child(2) > input");

I don't know what you want to do with the input at this point. If you want an Array of the values, do this.

我不知道你现在想要做什么输入。如果需要值的数组,请执行此操作。

var vals = inputs.map(function() { 
                         return this.value; 
                      }).toArray();

#2


0  

you can try this:

你可以试试这个:

var values = [];
$('#urlList .urlvalue').each(function(){
    var val = $(this).val();
    values.push(val);
});

The array values have all input values from your table.

数组值包含表中的所有输入值。