我如何知道 jquery 中单击了哪一行
我有一个表,其中包含服务器端脚本(PHP)动态的行 并且还使用 Zend Framework 。 所以每一行都会有带有 id 的超链接 SOmevalue(动态) 现在,每一行都将通过以下代码将上下文菜单(Jquery 插件)附加到每一行。
$("#myTable td:first-child").contextMenu("myMenu1",{
bindings: {
'hdr': function(t) {
alert($("#Test0").attr("data-value1"));
alert($("#Test0").attr("data-value2"));
}
},
menuStyle: {
border: '1px solid #000'
}
});
所以我的问题是我如何知道上下文菜单单击了哪一行 假设如果我单击第二行,我想获取该值 <一个href> “test1”或“test2”或“test3”或“test4”的值
动态地。我不应该在上面写“test1”或test2
I have table which contains the rows that are dynamic from server side scripting(PHP)
and also using Zend Framework .
So each row will have hyperlink with id
SOmevalue(Dynamic)
now each row will have contextmenu (Jquery Plugin ) appended to each row by the below code.
$("#myTable td:first-child").contextMenu("myMenu1",{
bindings: {
'hdr': function(t) {
alert($("#Test0").attr("data-value1"));
alert($("#Test0").attr("data-value2"));
}
},
menuStyle: {
border: '1px solid #000'
}
});
So My Problem is how do i know on which row was context menu clicked and
suppose if i click on second row i want to get the value of that < a href>
value of that "test1" or "test2" or "test3" or "test4"
dynamically . i should not able to write "test1"or test2 son on
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与大多数 jQuery 事件处理程序一样,我的猜测是
this
变量将是被单击的元素。无论如何,如果您至少可以获得被单击的元素,那么其余的应该很容易。查看 API 文档中的
closest()
方法:http://api. jquery.com/closest/我只是在这里猜测,但你可以做类似的事情
var value = $(this).closest("a.someClass").attr("data-value1");
其中“someClass”是可选的,但有助于确保您获得正确的锚点如果同一行中可能还有其他人,则进行标记。AS with most jQuery event handlers, my guess is that the
this
variable will be the element that was clicked. Regardless, if you can at least get the element that was clicked, the rest should be easy.Check out the
closest()
method in the API docs: http://api.jquery.com/closest/I'm just guessing here, but you could do something like
var value = $(this).closest("a.someClass").attr("data-value1");
where "someClass" is optional but would help to make sure you got the right anchor tag if there were potentially others in the same row.请提供生成的 html 标记。
沿着这些思路的东西应该有效:
Please provide the html markup that gets generated.
Something along these lines should work: