如何将jQuery类应用于通过AJAX响应生成的HTML元素,从而使jQuery功能能够工作


How to apply the jQuery classes to HTML elements generated through AJAX response so that the jQuery functionality should be workable?

我使用PHP, jQuery为我的网站。我有以下HTML元素,当页面加载时出现在页面上。

//Date picker controls
<input class="form-control date_control" placeholder="yyyy-mm-dd" type="date" name="rebate_start_date[1]" id="rebate_start_date_1" value="">
<input class="form-control date_control" placeholder="yyyy-mm-dd" type="date" name="rebate_expiry_date[1]" id="rebate_expiry_date_1" value="">
//Select control
<select class="states" multiple="multiple" name="applicable_states[1]" id="applicable_states_1">
  <option value="1">Alabama</option>
  <option value="2">Alaska</option>
  <option value="3">Arizona</option>
  <option value="4">Arkansas</option>
  <option value="5">California</option>
</select>

在上面的代码中,我已经为HTML控件添加了jQuery类,以使jQuery功能可行。

  1. 。date_control为两个日期选择器控件
  2. 。select control

使用上述指定类的HTML元素的jQuery代码如下:

$(document).ready(function() {
  //code for datepicker
  $(".date_control").datepicker({
    dateFormat: "yy-mm-dd"
  });
  //code for states
  $('.states').multiselect({
    includeSelectAllOption: true,
    maxHeight: 150
  });
});

现在点击页面上的一个按钮,我调用AJAX函数如下。

<button style="float:right" class="add_new_rebate" type="button" class="btn btn-default" onclick="add_rebate_by_product(); return false;"><i class="icon-plus"></i> &nbsp;Add New Rebate</button>

然后在AJAX函数我给调用PHP文件。在PHP文件中,我正在做出响应并将其发送回AJAX请求。在此之前一切都很好。但我面临的问题是,jQuery功能在我通过AJAX响应添加的HTML控件上不起作用。在准备PHP响应时,我已经注意添加了与上面相同的类。即使我通过检查各自的HTML元素来检查源HTML,那里仍然存在jQuery类,但功能仍然无法正常工作。为了供您参考,我在下面给出了AJAX请求代码和PHP文件中的响应准备代码:

//AJAX request code
function add_rebate_by_product() { 
    var manufacturer_id =  $("#company_id").val();
    var next_rebate_no = $('.rebate_block').length + 1;
    var rebate_no      = $('.rebate_block').length + 1;
    if ($('.rebate_block').length>0) { 
      rebate_no = rebate_no+1;
    }
      $('.add_new_rebate').attr('disabled','disabled');

    $.ajax({
      type: "POST",
      url: "add_rebate_by_product.php",
      data: {'request_type':'ajax', 'op':'create_rebate', 'next_rebate_no':next_rebate_no, 'rebate_no':rebate_no, 'manufacturer_id':manufacturer_id},  
      beforeSend: function() { 
        $('.table-responsive').after("<img src='http://localhost/smart-rebate-web/web/img/ajax-loader.gif' class='load' alt='Loading...'>");
      },
      success: function(data) {
        if(jQuery.trim(data)=="session_time_out") {
        window.location.href = site_url+'admin/login.php?timeout=1';                
        } else {
          $('.rebate_block').append(data);
          $('.add_new_rebate').removeAttr('disabled');
        }
        $('.load').remove();
      }
    });
}

//PHP code snippet to prepare response
    <?php
    $op = $_REQUEST['op'];
    switch( $op ) {
     case "create_rebate": 
    echo "<input class='form-control date_control' placeholder='yyyy-mm-dd' type='date' name='rebate_start_date[$rebate_no]' id='rebate_start_date_$rebate_no' value=''><input class='form-control date_control' placeholder='yyyy-mm-dd' type='date' name='rebate_expiry_date[$rebate_no]' id='rebate_expiry_date_$rebate_no' value=''>
    <select class='states' multiple='multiple' name='applicable_states[$reabate_no]' id='applicable_states_$reabate_no'>
      <option value='1'>Alabama</option>
      <option value='2'>Alaska</option>
      <option value='3'>Arizona</option>
      <option value='4'>Arkansas</option>
      <option value='5'>California</option>    
    </select>";
    exit;
    }
    ?>

我在谷歌上搜索了很多,但仍然没有找到一个完美的解决方案,可以使jQuery功能适用于使用AJAX添加的HTML控件。有人能在这方面帮助我吗?感谢您花费宝贵的时间来理解我的问题。如果你需要任何关于这个问题的信息,我也可以提供给你。任何形式的帮助,评论,建议,答案将非常感谢。等待您宝贵的回复

将初始化代码封装在函数中:

function initializeControls(){
  //code for datepicker
  $(".date_control").datepicker({
    dateFormat: "yy-mm-dd"
  });
  //code for states
  $('.states').multiselect({
    includeSelectAllOption: true,
    maxHeight: 150
  });
}

然后在ajax回调中调用它:

success: function(data) {
    if(jQuery.trim(data)=="session_time_out") {
    window.location.href = site_url+'admin/login.php?timeout=1';                
    } else {
      $('.rebate_block').append(data);
      $('.add_new_rebate').removeAttr('disabled');
      initializeControls();
    }
    $('.load').remove();
 }