在其他Select2下拉更改事件上设置Select2下拉值

时间:2016-02-02 17:01:56

标签: javascript jquery drop-down-menu

我正在尝试在其他Select2下拉列表更改事件中设置Select2下拉值..

这些是我的下拉列表

<div >
        @Html.DropDownList(Model.deptProp, new SelectList(Bmsa.UI.Utility.Utility.GetAllDepartments(Model.countryCode), "Value", "Text", Model.deptId), new { onchange = "Getarrnds();" })

</div>

 <div>
                @Html.DropDownList(Model.cmnlSctnProp, new SelectList(Bmsa.UI.Utility.Utility.GetCmnlSection(Model.countryCode, Model.deptId, Model.arrndsId, Model.communeId), "Value", "Text", Model.cmnlSctnId))

        </div>

脚本

<script type="text/javascript">
    $(function () {

        var $ddlSub = $("select[name$=deptId]");
        $ddlSub.select2();

        var $ddlcmnlSctn = $("select[name$=cmnlSctnId]");
        $ddlcmnlSctn.select2();

    });

</script>

这是两个下拉列表的HTML解析

<span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-deptId-container"><span class="select2-selection__rendered" id="select2-deptId-container" title="Select below">Select below</span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span>

<span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-cmnlSctnId-container"><span class="select2-selection__rendered" id="select2-cmnlSctnId-container" title="Select below">Select below</span><span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span>

我试过以下但是这些都没有用

 $("select[name$=cmnlSctnId]").change(function () {
            $("select[name$=deptId]").select2("val","Ouest").val('Ouest').trigger("change");

$("select[name$=deptId]").select2("val","Ouest").val('2').trigger("change");

$("select[name$=deptId]").select2("val", "Ouest"); 

            $("select[name$=deptId]").val("2").trigger("change");
 $('select[name$=deptId]').select2('data', { id: 100, a_key: 'Lorem Ipsum' });
 $('select').select2().select2('val', $('.select2 option:eq(3)').val());

        });

..如何查看我遗失的地方?

1 个答案:

答案 0 :(得分:1)

您需要按如下方式附加click事件,并使用val方法设置值

对于E.x

var e1 = $("select[name$=deptId]").select2();

var e2 = $("select[name$=cmnlSctnId]").select2();

e1.on("click", function () { alert("test"); e2.val("2").trigger("change"); });

请检查JSFiddle以获取select2选项 https://jsfiddle.net/6ncs0vvh/