Jquery Select2动态制作

时间:2016-01-25 07:19:55

标签: javascript php jquery select2

我想在单击加号按钮时生成Jquery select 2。见下面的截图

enter image description here

附加功能和删除行的功能非常好。

我正在使用display none初始化一个div来附加HTML。

如下,,,

<div class="newbrndsrs" style="display:none">
    <div class="row">
        <div class="col-md-5" style="margin-bottom: 15px;">
            <div class="form-group">
                <label for="" class="col-lg-4 col-sm-4" style="text-align: right;">Trade/Brand Name</label>
                <div class="col-lg-8 col-sm-8 prepend-icon">
                    <select name="brandfk[]" class="form-control form-white modlfetch" data-placeholder="Trade/Brand Name">
                        <option value=""></option>
                            <?php
                            while($row9=mysql_fetch_assoc($brandfetchAjax)){ ?>
                                <option value="<?php echo $row9['ID'];?>"><?php echo $row9['BrandName'];?></option>
                            <?php } ?>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-md-5" style="margin-bottom: 15px;">
            <div class="form-group">
                <label for="" class="col-lg-4 col-sm-4" style="text-align: right;">Series Name</label>
                <div class="col-lg-8 col-sm-8">
                    <select name="brand[]" class="form-control form-white slctjpmodelname">
                    </select>
                </div>
            </div>
        </div>
        <span class="btn btn-primary addline"><i class="glyphicon glyphicon-minus"></i></span>
    </div>
</div>

单击按钮加上我正在使用上面的HTML并将其绑定到加号按钮的底部。

  <div class="row apndarea">
                                <div class="col-md-5" style="margin-bottom: 15px;">
                                    <div class="form-group">
                                        <label for="" class="col-lg-4 col-sm-4" style="text-align: right;">Trade/Brand Name</label>
                                        <div class="col-lg-8 col-sm-8 prepend-icon">
                                            <select name="brandfk[]" class="form-control form-white modlfetch" data-placeholder="Trade/Brand Name">
                                                <option value=""></option>
                                                <?php
                                                while($row2=mysql_fetch_assoc($brandfetch)){ ?>
                                                <option value="<?php echo $row2['ID'];?>"><?php echo $row2['BrandName'];?></option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-5" style="margin-bottom: 15px;">
                                    <div class="form-group">
                                        <label for="" class="col-lg-4 col-sm-4" style="text-align: right;">Series Name</label>
                                        <div class="col-lg-8 col-sm-8">
                                            <select name="brand[]" class="form-control form-white slctjpmodelname">
                                                <option value="">Series </option>
                                                <option value="">Series </option>
                                                <option value="">Series </option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <span class="btn btn-primary addline"><i class="glyphicon glyphicon-plus"></i></span>
                            </div>

... Jquery的

  $(".addline").click(function(){
                var eqtype      = $("select[name=EquipmentTypeID]").val();
                if(eqtype == ""){
                    $("select[name=EquipmentTypeID]").next().show();
                    return false;
                }else{
                    $(".form-error").hide();
                    $(".apndarea").after($(".newbrndsrs").html());
                }

            });

但New Dynamic Select2不工作....

我无法点击它......

问题是什么?

2 个答案:

答案 0 :(得分:2)

$(document).on('click',".addline", function(){
                var eqtype      = $("select[name=EquipmentTypeID]").val();
                if(eqtype == ""){
                    $("select[name=EquipmentTypeID]").next().show();
                    return false;
                }else{
                    $(".form-error").hide();
                    $(".apndarea").after($(".newbrndsrs").html());
                }

            });

点击未绑定到动态创建的对象,请使用on

答案 1 :(得分:0)

使用事件分组

 $(document).on('click', ".addline", function () {
            var eqtype = $("select[name=EquipmentTypeID]").val();
            if (eqtype == "") {
                $("select[name=EquipmentTypeID]").next().show();
                return false;
            } else {
                $(".form-error").hide();
                $(".apndarea").after($(".newbrndsrs").html());
            }

        });