引导双线框如何获得选择的值

bootstrap dualistbox how get values selected

本文关键字:选择 何获得 线框      更新时间:2023-09-26

我使用这个插件为引导3 https://github.com/Geodan/DualListBox,这个例子https://jsfiddle.net/hh2zrt82/我试图得到一个选定的值,但我不能,我怎么能得到一个选定的值。谢谢你的帮助。

 var data = [
        {
            "id": 56,
            "countryList_id": 10,
            "port_iso": "India-pok"
        },
        {
            "id": 57,
            "countryList_id": 10,
            "port_iso": "India-nmp"
        },
        {
            "id": 58,
            "countryList_id": 10,
            "port_iso": "India-cop"
        },
        {
            "id": 61,
            "countryList_id": 2,
            "port_iso": "Bangladesh-dhp"
        },
        {
            "id": 62,
            "countryList_id": 2,
            "port_iso": "Bangladesh-brp"
        },
        {
            "id": 63,
            "countryList_id": 3,
            "port_iso": "Singapore-pos"
        },
        {
            "id": 64,
            "countryList_id": 3,
            "port_iso": "Singapore-SIN"
        },
        {
            "id": 65,
            "countryList_id": 3,
            "port_iso": "Singapore-zxczx"
        },
        {
            "id": 66,
            "countryList_id": 2,
            "port_iso": "Bangladesh-qewrqwe"
        },
        {
            "id": 67,
            "countryList_id": 3,
            "port_iso": "Singapore-erer"
        },
        {
            "id": 68,
            "countryList_id": 3,
            "port_iso": "Singapore-SIN"
        }
    ];
    var port_ids = [65,66,67];
    var options = '';
                for (var i = 0; i < data.length; i++) {
                    var a = 1;
                    for (var j = 0; j < port_ids.length; j++) {
                        // Appending "selected" attribute to the values which are already selected
                        if (port_ids[j] == data[i]["id"]) {
                            options += '<option value="' + data[i]["id"] + '" selected="selected">' + data[i]["port_iso"] + '</option>';
                            a = 0;
                        }
                    }
                    if (a == 1) {
                        options += '<option value="' + data[i]["id"] + '">' + data[i]["port_iso"] + '</option>';
                    }
                }

                $("select#country-of-operation-edit").empty().append(options);
               $("#country-of-operation-edit").DualListBox();

使用jQuery,你可以这样做:

$('option[selected="selected"]')

你可能想给你的选项添加一个类,在这种情况下你可以这样做:

$('option.className[selected="selected"]')

为了获得选定的值,在您必须设置name属性之前:

<select class="form-control" name="country-of-operation-edit[]"  multiple="multiple" data-json="false" id="country-of-operation-edit">

之后可以得到值,所以:

$('[name="country-of-operation-edit[]"]').val()