1

I'm having trouble understanding how to pass a jQuery object between functions. I keep getting undefined values when trying to use val after passing jQuery objects to a function.

Very simple JS Fiddle example http://jsfiddle.net/BtVqc/1/

$(document).ready(function () {
    function doSomething(objectOne, objectTwo) {
        $('table').append('<tr><td>' + objectOne.val() + '</td><td>' + objectTwo.val() + '</td></tr>');
    }

    $('button').click(function () {
        var tdOne = $('td.yy');
        var tdTwo = $('td.xx');
        doSomething(tdOne, tdTwo);
    });
});
4

1 回答 1

5

The correct selector for the input is td .xx not td.xx

var tdOne = $('td .yy');
var tdTwo = $('td .xx');

http://jsfiddle.net/BtVqc/2/

td.xx means - a table cell that has xx class

td .xx means - some node that has xx class and is nested to a table cell

于 2013-04-26T10:46:28.527 回答