簡體   English   中英

動態加載的div上的jQuery Select2插件

[英]jQuery Select2 plugin on dynamicaly loaded div

我嘗試加載一個具有帶有select2插件的列表框的div。 以下代碼用於div容器

<div class="form-body">
    <div class="form-group">
        <?php echo lang('project_product', 'product', array('class' => 'col-sm-2 control-label')); ?>
        <div class="col-sm-2">
            <select id="id_product" name="id_product" tabindex="4" class="form-control select2 id_product"></select> 
        </div>
    </div>       

    <div class="form-actions pull-right">
        <a href="javascript:;" id="cancel_product" class="btn btn-default">
            Cancel
        </a>&nbsp; 
        <a href="javascript:;" id="add" class="btn btn-success">
            <i class="fa fa-plus"></i> Add product
        </a>            
    </div> 
</div>   

div容器通過按鈕上的on click事件加載:

$(document).on('click', '#add_product', function(e){
    $('#add_p_container').removeClass('hide');
    $('#add_p_container').load(JS_BASE_URL + 'admin/projects/add_product/' + id_project);       
});

問題是,如果我從下面的js文件中加載jQuery文件,其中#add_product的on click事件對select2 #add_product

我用來初始化select2的代碼:

$("#id_product").select2({
    minimumInputLength: 2,
    placeholder: "Select a product",
    allowClear: true,
    quietMillis: 100,
    ajax: {
        url: JS_BASE_URL + 'admin/products/productsList',
        dataType: 'json',
        type: "GET",
        quietMillis: 50,
        data: function (params) {
          return {
            q: params.term, // search term
            page: params.page
          };
        },
        processResults: function (data, params) {
          // parse the results into the format expected by Select2
          // since we are using custom formatting functions we do not need to
          // alter the remote JSON data, except to indicate that infinite
          // scrolling can be used
          params.page = params.page || 1;

          return {
            results: data,
            pagination: {
              more: (params.page * 30) < data.total_count
            }
          };
        },
        cache: true
    },
    escapeMarkup: function(markup) {
        return markup;
    } // let our custom formatter work

}); 

如果在div中將select2直接從上面加載時,我將收到錯誤消息:

 [Show/hide message details.] SyntaxError: expected property name, got end of script

你們當中有人知道如何解決嗎? 謝謝!

在嘗試尋找解決問題的方法之后,似乎可以將腳本加載到div中,但是刪除所有我不知道為什么的注釋,但是可以工作

因此,請確保您沒有留下任何評論,否則它將無法正常工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM