简体   繁体   English

jQuery单击功能不起作用

[英]jQuery Click Function Not Working

jQuery click function not working properly. jQuery单击功能无法正常工作。 Actually it works but I'm loading my HTML via JavaScript call after pageload, in this condition jQuery click function not working. 确实可以,但是我要在页面加载后通过JavaScript调用加载HTML,在这种情况下jQuery click函数无法正常工作。

HTML part: HTML部分:

<table class="table table-lg">
   <thead>
      <tr>
         <th width="5%" class="text-center">#</th>
         <th width="30%" class="text-center"><?php echo lang('suggestion'); ?></th>
         <th width="10%" class="text-center"><?php echo lang('category'); ?></th>
         <th width="15%" class="text-center"><?php echo lang('proposer'); ?></th>
         <th width="14%" class="text-center"><?php echo lang('date'); ?></th>
         <th width="10%" class="text-center"><?php echo lang('likes'); ?></th>
         <th width="16%" class="text-center"><?php echo lang('action'); ?></th>
      </tr>
   </thead>
   <tbody id="get_sgs">

<!-- This part is loading by jQuery POST -->
   </tbody>
</table>

JavaScript: JavaScript:

    <script type="text/javascript">
    window.onload = function() {
        $.ajax({
          url: "<?php echo site_url('get/suggestions'); ?>",
          success: function(data){
            $("#get_sgs").html(data);
          }
        });
    };
    $( "#reload_suggestions" ).click(function() {
        $.ajax({
            url: "<?php echo site_url('get/suggestions'); ?>",
            success: function(data){
                $("#get_sgs").html(data);
            }
        });
    });
    $('.rate_up').click(function() {
   var id = $(this).attr('data-id');
     console.log(id);
   $.ajax({
     url: '<?php echo site_url('rate/up'); ?>' + '/' + id,
     type: 'POST',
     data: {
       'submit': true,
     },

   });
 });
 $('.rate_down').click(function() {
     var id = $(this).attr('data-id');
     console.log(id);
     $.ajax({
         url: '<?php echo site_url('rate/down'); ?>' + '/' + id,
         type: 'POST',
         data: {
             'submit': true,
         },

     });
 });
</script>

Thanks in advance. 提前致谢。

You have two options: 您有两种选择:

  1. Bind the click handlers directly after $("#get_sgs").html(data); 将点击处理程序直接绑定到$("#get_sgs").html(data); inside the success block. success块内。
  2. Bind the click handlers using $("#get_sgs").on("click", ".rate_down", function() { 使用$("#get_sgs").on("click", ".rate_down", function() {

See Event binding on dynamically created elements? 请参阅对动态创建的元素进行事件绑定? for a description of the second option. 有关第二个选项的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM