jQuery——从表的最后一行获取输入字段值?

时间:2021-05-23 20:37:26

I'm a little confused on how to do this...

我有点搞不清该怎么做……

I have a table that has a series of rows, and within each cell, there are certain form elements. I'm trying to get the value from the "code" input field from the last row only, and am having trouble with the syntax...

我有一个表,它有一系列的行,在每个单元格中,有一些特定的表单元素。我试图从最后一行的“代码”输入字段中获取值,但是语法有问题……

Simplified table looks like this:

简化表如下:

<table id="table1">
<tr><td><input type="hidden" name="code" value="XFT" /></td></tr>
<tr><td><input type="hidden" name="code" value="ETY" /></td></tr>
<tr><td><input type="hidden" name="code" value="DHQ" /></td></tr>
</table>

And, here's the jquery that doesn't work...

这是jquery不能工作的……

if($('#cont')) {
            $("#cont').live('click', function(event) {
                var tr = $('#wr-viewcarttable tr:last');
                var itemcode = $(tr > 'input[name="code"]').val();
                window.location = "/search?p="+itemcode;
            });
        }

1 个解决方案

#1


11  

Try this:

试试这个:

$('table#table1 tr:last input[name=code]').val();

Or, adjusted to your code:

或者,根据您的代码进行调整:

$('#cont').live('click', function(event) {
    var tr = $('#wr-viewcarttable tr:last');
    var itemcode = tr.find('input[name=code]').val();
    window.location = "/search?p="+itemcode;
});

You have two errors in your code, you have mismatched quotes in the $("#cont') part and your input search is wrong. What you have now is:

您的代码中有两个错误,您在$(“#cont”)部分中有不匹配的引号,您的输入搜索错误。你现在拥有的是:

$(tr > 'input[name="code"]').val();

As the > is outside quotes, it is not a string, but a comparison operator, which now compares tr to 'input[name="code"]'. Comparison operators always return boolean values (true or false), so you're effectively doing this:

由于>是外引号,所以它不是一个字符串,而是一个比较操作符,现在它将tr与'input[name="code"]进行比较。比较运算符总是返回布尔值(真或假),因此您可以有效地这样做:

$(true).val();

Which doesn't make much sense. If you have a jQuery object, you can use the find method to find any children of that object, or alternatively pass the element as context to the $() function. So these two will work and are equal:

这没什么意义。如果您有jQuery对象,则可以使用find方法查找该对象的任何子元素,或者将元素作为上下文传递给$()函数。所以这两个是相等的

tr.find('input[name=code]').val();
$('input[name=code]', tr).val();

There's actually no reason to save the tr to it's own variable in your case, as you can get the value in just one declaration, as shown above.

实际上,没有理由将tr保存为自己的变量,因为您可以在一个声明中获得值,如上所示。

#1


11  

Try this:

试试这个:

$('table#table1 tr:last input[name=code]').val();

Or, adjusted to your code:

或者,根据您的代码进行调整:

$('#cont').live('click', function(event) {
    var tr = $('#wr-viewcarttable tr:last');
    var itemcode = tr.find('input[name=code]').val();
    window.location = "/search?p="+itemcode;
});

You have two errors in your code, you have mismatched quotes in the $("#cont') part and your input search is wrong. What you have now is:

您的代码中有两个错误,您在$(“#cont”)部分中有不匹配的引号,您的输入搜索错误。你现在拥有的是:

$(tr > 'input[name="code"]').val();

As the > is outside quotes, it is not a string, but a comparison operator, which now compares tr to 'input[name="code"]'. Comparison operators always return boolean values (true or false), so you're effectively doing this:

由于>是外引号,所以它不是一个字符串,而是一个比较操作符,现在它将tr与'input[name="code"]进行比较。比较运算符总是返回布尔值(真或假),因此您可以有效地这样做:

$(true).val();

Which doesn't make much sense. If you have a jQuery object, you can use the find method to find any children of that object, or alternatively pass the element as context to the $() function. So these two will work and are equal:

这没什么意义。如果您有jQuery对象,则可以使用find方法查找该对象的任何子元素,或者将元素作为上下文传递给$()函数。所以这两个是相等的

tr.find('input[name=code]').val();
$('input[name=code]', tr).val();

There's actually no reason to save the tr to it's own variable in your case, as you can get the value in just one declaration, as shown above.

实际上,没有理由将tr保存为自己的变量,因为您可以在一个声明中获得值,如上所示。