简体   繁体   中英

Getting selected items count ListBox with jQuery

As in topic - how to get selected items count in ListBox with jQuery, while user is selecting new item?

I have these code:

@Html.ListBoxFor(x => Model.StatesID, Model.States,
        new { @class = "chzn-select", @id="StatesID", data_placeholder = "Choose...", style = "width:350px;" })


function countStates() {
    var count = $("#StatesID:selected").length;
    alert(count);

}

I can get count of selected items in onclick input action, but how to connect this js function with onclick event, or maybe other event, when user is selecting new item in my listbox?

Adding my comment as answer....

If it is a multi-select, you will want to use....

$('#StatesID').on('change', function(){
    countStates();
});

otherwise click does not fire if the user selects more that one option at a time.

Try this :

$(document).ready(function() {
    $('#StatesID').click(countStates);
});

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