jQuery on function is getting bound to the container, why so?

I have a simple jquery code $('tbody').on("click",$('vote'+rowCount),function(){alert('hi');}); but the alert shows up when i click anywhere on the table body instead of just showing up on clicking the selector Why is this happening? ----Edit--- Got it working by writing the element as "#vote"+rowCount instead of writing as $('vote'+rowCount) But there is another weird problem - I am creating dynamic rows with this code, but whatever i type in the value of the textbox remains empty (as initialized), why so $('#add_ans').click(function(){ var rowCount = $('table tbody tr').length + 1; var rowString = '\
'+rowCount+'
\
\
0
\
\
\ ' $('tbody').append(rowString); $('tbody').on("click","#vote"+rowCount,voteOption); }); and the event handler is function voteOption(){ var rowCount = $('tr').index($(this).closest('tr')); alert(rowCount); var ans = $('tr:eq('+rowCount+') .input-large').attr('value'); alert(ans); }
$('vote'+rowCount) would be matching invalid elements, and the argument should be a selector, not a jQuery wrapper. api.jquery.com/on

以上就是jQuery on function is getting bound to the container, why so?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏