获取所选项目使用jQuery计算ListBox [英] Getting selected items count ListBox with jQuery

查看:101
本文介绍了获取所选项目使用jQuery计算ListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在主题中 - 如何使用jQuery在ListBox中获取所选项目,而用户是否正在选择新项目?

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

我有这些代码:

@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);

}

我可以在<$ c $中获得所选商品的数量c> onclick 输入操作,但是当用户在我选择新项目时,如何将此js函数与 onclick 事件或其他事件连接起来listbox?

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.

这篇关于获取所选项目使用jQuery计算ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆