Drop-down list - jQuery event

I think it will be very simple for anyone who is decent with jQuery, but I have dynamically generated dropdownlists on my page and I set up a click event on the class, which, as expected, fires a click event for all of them. The identifier is selectdynamically generated, so I cannot use it as a selector. I just want to fire an event for selectthat has really been changed.

I need to use the id of the selected value in a function - does anyone know how to do this?

The code I still have:

$(document).ready(function () {
    $(".boxItem").change(function () {
        var str = $(this).attr('id');
        var num = $(this).val();
        alert(num);
        var url = "/Search/GetBoxChangeInfo";
        var vmData = { json : @Html.Raw(Json.Encode(Model)),
                        id : str,
                        boxNo : num };
        $.post(url, vmData, function (data) {
            $("#column-1").html(data);
        });
    });
});

Many thanks

+3
source share
2 answers
$(".boxItem").on("change", function () {

    // to get the value and id of selected option
    var str = $('option:selected', this).attr('id');
    var value = $('option:selected', this).attr('value');

    // or to get value and id of select box use
    var str = this.id; 
    var value = this.value;
    ......
});

If you selectare active, they are created after the DOM is ready, you should try

 $("body").on("change", ".boxItem", function () {

    // to get the value and id of selected option
    var str = $('option:selected', this).attr('id');
    var value = $('option:selected', this).attr('value');

    // or to get value and id of select box use
    var str = this.id; 
    var value = this.value;
    ......
});
+1

, :

$(document).on('change', '.boxItem', function () {
    var str = this.id;
    var num = this.value;
    ......

this .boxitem, , , !

+2

All Articles