简体   繁体   中英

Get the selected option of select2 using id of that option

Currently I am doing it without the select2 plugin and it works but I want to use select2 plugin

What I am trying to do here is get the selected option using the Select2 plugin and then do an ajax call

so if an option is selected the id will be passed and the ajax call will be done

$('.select2').change(function(e){         
    var arraypos = $(this).children(":selected").attr("id");
        jQuery.ajax({
            url: '/wallfly-mvc/public/dashboard/selectedProperty',
            type: "POST",
            data: {
                selected: arraypos
            },
            success: function (result) {
                $('#showoption').attr('id');
                window.location.reload();
            },
            error: function (result) {
                alert('Exeption:' + exception);
            }
        });
});

You will retrieve the selected option of a select2 input using jquery just the same you would do for a normal select input

http://jsfiddle.net/jEADR/2162/

<select id="someInputId" style="width:300px">
        <option value="AL">Alabama</option>
        <option value="Am">Amalapuram</option>
        <option value="An">Anakapalli</option>
        <option value="Ak">Akkayapalem</option>
        <option value="WY">Wyoming</option>
</select>


$("#someInputId").select2();

$('#someInputId').change(function(e){         
    console.log($(this).children(":selected").attr("value"));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM