jQuery - get an input field value from the last row in a table? -
I'm a bit confused at how to do this ...
I have a table with rows There is a series, and within each cell, there are certain elements, I'm just trying to get the value from the last line from the "row" input field, and there is a problem with syntax ...
The simplified table looks like this:
& lt; Table id = "table1" & gt; & Lt; Tr & gt; & Lt; Td> Input & Lt; / Td> & Lt; / Tr & gt; & Lt; Tr & gt; & Lt; Td> Input & lt; / Td> & Lt; / Tr & gt; & Lt; Tr & gt; & Lt; Td> Input & Lt; / Td> & Lt; / Tr & gt; & Lt; / Table & gt;
And, here is jquery that does not work ...
if ($ ('# cont')) {$ ("# cont" Click '' function '' {var tr = $ ('# wr-viewcarttable tr: last'); var item code = $ (tr & gt; 'input [name = "code"]'). ); Window; location = "/ search? P =" + ictod;});}
Try it:
$ ('Table # Table 1 tr: Last input [name = code]'). Val ();
Or, adjust to your code:
$ ('# cont'). Live ('click', function (event) {var tr = $ ('# wr-viewcarttable tr: last' ); Var itemcode = tr.find ('input [n You have two errors in your code, you have
The quotation marks in the code> $ ("# cont ') section are not matched and your input search is incorrect What do you have now:
$ (tr & gt; 'Input [name = "code"]'). Val (); In the form of and are extraneous quotations, it is not a string, but a comparison operator, which now returns tr
to 'input [ Name = "code"] '
. Comparison operators always return boolean values (true or false), so you are doing it effectively:
$ (true) .val ();
Which does not make much sense if you have a jQuery object, you can use the Find
method to search for any child of that object Alternatively, you can pass the element in the context of the $ ()
function. So both of them will work and will be the same:
tr.find ('input [name = code]'). Val (); $ ('Input [name = code]', tr) .val ();
In fact, in your case, there is no reason to save tr
in its own variable, just as you mentioned above, only one declaration Can get it. / P>
Comments
Post a Comment