开发者

javascript not working for newly added innerhtml through jquery

开发者 https://www.devze.com 2023-01-06 04:39 出处:网络
I have following HTML structure <table> <tr> <div id=\"getData\"> </div> </tr>

I have following HTML structure

<table>
<tr>
   <div id="getData">
   </div> 
</tr>
</ta开发者_高级运维ble>

I am using jquery's html() method to add dynamic contents to div id "getData" as:

  $('#valueResult').html(data);
    var data ="
<table><tr><td><input type="checkbox" name="test[1]" onclick="doSometing()" /> Test</tr></tr></table> 
    <table><tr><td><input type="checkbox" name="test[2]" onclick="doSometing()" /> Test</tr></tr></table> "  

But the function doSometing() is never executed for any of the dynamically added checkboxes. why so ?

can anyone help me out ?

THX.


A cleaner jQuery solution for a problem like this is to use .live() or even better .delegate().

With this approach you should also give your input element an unique id and call:

$(document).ready(function(){
   $('#unique_input_id').live('click', function(){
      alert('clicked');
   });
});

Reference: .live(), .delegate()

0

精彩评论

暂无评论...
验证码 换一张
取 消