doujiebo9849 2014-04-25 07:58
浏览 395
已采纳

在jquery中单击动态更改Label上的文本

My web html has following code

<script  src="js/jquery.flexslider.js"></script>
<script  src="js/jquery.rings.js"></script>

Contents of jquery.rings.js are :

$('input[type="image"]').click(function()
{
   $ring = $(this).data('my-info');
   $('label#ringName').text($ring['ringSetName']);
});


/***************************************************/

My Html code is fetching an array of rings

$db = new db();
$rings = $db->query("SELECT * FROM rings");

On runtime i am adding images in a slider as follow

<div class="albums-div">
    <ul class="slides">

        <?php



 foreach($rings as $ring)
          {
             echo '<li> <input type="image" src="' . $ring['ringThumbNailImagePath'] . '" name="checked" value="" data-my-info="' . $ring . '" width="150" height="150" /></li>';
           }   



         ?>
    </ul>
</div>

The problem is , on Clicking the image nothing happens, the JS script seems not to be getting called.

tHE JS function should set the text of a label defined in the code as

What could be the problem?

  • 写回答

2条回答 默认 最新

  • dongqiao1888 2014-04-25 07:59
    关注

    You need event delegation for dynamically added elements:

    $(document).on('click','input[type="image"]',function()
    {
      $ring = $(this).data('my-info');
      $('label#ringName').text($ring['ringSetName']);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?