jquery click event not working on first click,
$("#table").click(function(e) {
var row = jQuery(e.target || e.srcElement)开发者_Python百科.parent();
$('#table tr').bind('click', show);
name= row.attr("id");
});
I am not getting the id value very first time i click on the row? second time I am getting fine? can anyone tell me why its happening like this?
It looks like you're binding an event to a row when you click on the table. This will mean that the onclick
handler you're binding to the row won't be bound (and therefore, won't be executed) until after you've clicked on the table at least once.
But maybe I don't fully understand what you're trying to do. Could you explain what your ultimate goal here is?
Just put your click
function within (document).ready()
like:
$(document).ready(function(){
$("#table").click(function(e) {
var row = jQuery(e.target || e.srcElement).parent();
$('#table tr').bind('click', show);
name= row.attr("id");
});
});
It will work then.
I think the problem is arrising because you have nested your click handlers.Try to do it without nesting them and I thonk you will get the desired result.
Well, for one thing, you might not be firing the event on a TD
, but it could be anything inside that TD
, such as a SPAN
. That would make the parent()
something other than the row you expect it to be. Try using $(e.target).closest("TR")
.
Also, in JQuery you don't need to use e.srcElement, it will normalize the event object for you.
EDIT: Your question still isn't very clear to me, but based on your comments you want to pop up the row id whenever you click on a row:
$("#table").click(function(e) {
var row = $(e.target).closest("TR");
name=$(row).attr("id");
show(row);
});
However, this is not consistent with your comment below where you say you want the second click to show the second row, the third click to show the third row, etc.
精彩评论