如何从文本旁边的文本框中获取值
我有一个html页面,其中有一个项目表(每行一个项目),其中每个项目都是一个html链接,上面写着添加,每个链接旁边都有一个文本框,里面有一个数字。
i have an html page where i have a table of items (one item per row) where each item is a html link that says "Add" and there is a textbox next to each link with a number in it.
文本链接和文本框位于表格行内的同一个td。
the text link and the textbox are in the same td inside the row of the table.
我怎么做,使用jquery,从我点击的链接右侧的文本框中捕获值。
how do i, using jquery, capture the value from the textbox that is to the right of the link i click on.
你可以给你的链接一个班级,然后这样做:
You could give your link a class, then do this:
$("a.myClass").click(function() {
alert($(this).next().val());
});
如果您有很多链接,或者它们是动态添加的,请稍微更改一下 .live()
,如下所示:
If you have a lot of links, or they're dynamically added, change it a bit to use .live()
, like this:
$("a.myClass").live('click', function() {
alert($(this).next().val());
});
如果你想移动文本框,中间有一个元素等,但它仍然是只在单元格中输入,你可以改变这个: $(this).next()。val()
这样的事情:
If you wanted to move the textbox around, have an element in-between, etc but it's still the only input in the cell, you could change this: $(this).next().val()
to something like this:
$(this).closest('td').find(':text').val()