Magicsuggest以编程方式更改选择

时间:2017-04-10 17:41:49

标签: selection magicsuggest

我一直在使用MagicSuggestion几天:

<div>
     <div id="magicsuggest"></div>
 </div>

VehicleNameList = [{id: 1, name: "Volvo"},
                   {id: 2, name: "Audi"},
                   {id: 3, name: "Volkswagen"}]

vehicleNameSelected = "Volvo"

magicVehicleChange = $('#magicsuggest').magicSuggest({
                        data: VehicleNameList,
                        placeholder: function () {
                            $('.ms-sel-ctn input').attr("placeholder", vehicleNameSelected);
                        }
                    });


 $(magicVehicleChange).on('selectionchange', function (e, m, sel) {
                    if (sel.length == 0) return;
                    for (i = 0; i < VehicleNameList.length; i++) {
                        if (typeof sel[0] != 'undefined')
                            if (VehicleNameList[i].name == sel[0].name) {
                                vehicleNameSelected(sel[0].name);
                                break;
                            }
                    }
                    if (typeof sel[0] != 'undefined') {
                        magicVehicleChange.collapse();
                        this.clear();
                    }
                });

问题是我想在更改按钮时更改选定的车辆名称,例如更改为“Volkswagen”,通过调用:

magicVehicleChange.setValue([{ name: "Volkswagen", value: 3 }])

但没有成功。我做错了什么?有没有办法在按下按钮时更改选择?

1 个答案:

答案 0 :(得分:0)

我认为你需要改变:

magicVehicleChange.setValue([{ name: "Volkswagen", value: 3 }])

magicVehicleChange.setSelection([{ id: 3, name: "Volkswagen" }])