【问题标题】:set selected value in dropdown when update data in laravel using ajax使用 ajax 在 laravel 中更新数据时在下拉列表中设置选定值
【发布时间】:2021-11-08 12:34:55
【问题描述】:

我有这样的下拉菜单:

                <div class="row">
                    <div class="col-md-12 col-sm-6">
                        <div class="form-group">
                            <label for="">{{ __('Product Plan') }}<span class="required"></span></label>

                            <select name="product_plan" class="form-control" id="product_plan" style="width: 50%">
                                @foreach ($product_plan as $val)
                                    <option value="{{ $val->productplanID }}">
                                        {{ $val->productplanID }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                </div>

我使用 modal 来更新这些数据,所以我使用 AJAX。这是我获取数据的ajax方法:

$('body').on('click', '.editPlanSchedule', function() {
var Item_id = $(this).data('id');
$.get("/quotation/getEditPlanSchedule" + '/' + Item_id, function(data) {
    $('#product_plan').val(data.product_plan);
    // console.log(data.product_plan);
})

});

我正确获取了数据。我的问题是,如何使用 ajax 将所选值放入数据库中的选择选项中?我尝试 console.log(data.product_plan),我得到了数据。但是当我做 $('#product_plan').val(data.product_plan); 时,选择选项没有从数据库中选择正确的选择。

【问题讨论】:

标签: javascript jquery ajax laravel


【解决方案1】:

试试这个:

$('body').on('click', '.editPlanSchedule', function() {
var Item_id = $(this).data('id');
$.get("/quotation/getEditPlanSchedule" + '/' + Item_id, function(data) {
    let options = "";
    data.product_plan.forEach(p => {
        options += `<option value="${p.productplanID}">${p.productplanID}</option>`;
    }
    $('#product_plan').val(options);
})
});

【讨论】:

    猜你喜欢
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 2016-11-18
    • 2014-06-17
    相关资源
    最近更新 更多