首页 > 解决方案 > v-for 用于在 ajax 中使用时使用循环和 select2 不起作用

问题描述

为什么我的脚本不起作用。我从 ajax 获取数据并使用 select2 来选择产品,当我尝试使用 v-for 为在 prev item 中选择的每个产品添加部分时使用循环。

这是我的html:

<div class="item form-group col-md-3 green box" style="display: none">
    <label class="control-label" for="name">محصولات سفارشی<span class="required">*</span>
    </label>

    <select v-model="products_order" class="form-control reagent_selector" name="products_orders[]" multiple="multiple" id="">
        @foreach ($products as $item)
            <option value="{{$item->id}}">{{$item->name}}</option>
        @endforeach
    </select>
    <textarea v-model="products_order_desc" v-if="products_order.length > 0" name="products_order_desc" cols="10"
              rows="4" class="form-control" style="margin-top: 13px;" placeholder="توضیحات"></textarea>
</div>
<div class="col-md-3">
    <div v-for="(order,indexx) in products_order">
        <div class="item form-group">
            <div>
                <label class="control-label" for="name">منبع سفارش : @{{getProductById(order)}} <span
                        class="required">*</span>
                </label>
                <select v-model="selectedRes[order]" style="height: 43px;" class="form-control" name="" id="">
                    <option v-for="(item,index) in resources" :value="item.id">@{{item.name}} : @{{item.resource}}
                    </option>
                </select>
            </div>
        </div>
    </div>
</div>

这是 vue,js

products_order: function () {
    let self = this;
    let validator;
    let irt;
    if (self.products_order.length == 0) {
        self.selectedRes = {}
    }
    Object.entries(self.selectedRes).forEach(function (item, ir) {
        // console.log(item[0])
        validator = false;
        irt = null
        self.products_order.forEach(function (itemm, indexx) {
            if (itemm == item[0]) {
                validator = true;
                irt = itemm
            }
        })
        if (validator == true) {
            alert(irt)
            delete self.selectedRes[irt];
        }
    });
}

标签: javascriptajaxlaravelvue.jsjquery-select2

解决方案


推荐阅读