Javascript 如何统计列表中未选中的项目

Javascript 如何统计列表中未选中的项目,javascript,Javascript,在这些代码中,我可以得到选中的项目,那么未选中的项目呢 <select multiple="multiple" class="multi-select" id="clientIdList" name="clientIdList"> @{ foreach (var client in Model.ClientLists)

在这些代码中,我可以得到选中的项目,那么未选中的项目呢

 <select multiple="multiple" class="multi-select" id="clientIdList" name="clientIdList">
                           @{
                               foreach (var client in Model.ClientLists)
                               {
                                   <option @(client.IsChecked ? "selected" : "") value="@client.ClientId">@client.ClientName</option>
                               }
                           }
                       </select>

     var foo = document.getElementById('clientIdList');
    if (foo) {
        if (foo.selectedIndex < 0) {
            ShowError('Please select client', 'divNotiMailBoxSetting');

            return false;
        }

@{
foreach(Model.clientlist中的var客户端)
{
@client.ClientName
}
}
var foo=document.getElementById('clientIdList');
如果(foo){
如果(foo.selectedIndex<0){
淋浴ROR(“请选择客户端”、“divNotiMailBoxSetting”);
返回false;
}

您可以使用长度属性:

foo.selectedIndex.length

元素获取未选择的
项目数的一种方法:

// get all the option elements from the select element with the given id:
var opts = document.getElementById('clientIdList').options,
// create an object of arrays to contain the selected and unselected elements:
    choices = {
        'selected' : [],
        'unselected' : []
    };

// using Array.prototype.forEach to iterate over the NodeList of option elements:
[].forEach.call(opts, function (optElement) {
    // adding the current optElement to the selected, or unselected, array depending
    // on whether it's selected or unselected:
    choices [optElement.selected ? 'selected' : 'unselected'].push(optElement);
});

// assigning the length of the array of unselected elements to a variable for use,
// or to be returned to the calling context (if this is used inside of a function):
var unselectedElements = choices.unselected.length;
但是,我不知道您在做什么,我不得不让您在函数中实现它;不过,本文演示了一个简单的用例

参考资料:


具有
'clientIdList'
id
的元素是什么?一个列表(
)或者一个
元素?顺便说一句,如果您显示HTML,您可以更轻松地获得答案。@DavidThomas谢谢;现在请将您的问题包含在相关的HTML中。@DavidThomas@{foreach(Model.clientlist中的var client){@client.ClientName}}否:您的问题是包含相关的HTML。在(两者)中都有一个链接这不是HTML,这是一种生成HTML的脚本语言。当您右键单击并“查看源代码”时,您会看到什么?这是我们需要看到的。我很高兴能够提供帮助;但在将来,请花时间将您的问题提出来重新理解(并预先提供相关的代码),为你和那些试图帮助的人节省时间。如果回答了你的问题,请考虑接受这个答案,但这不是强制性的,应该被奖励给你最好或最好的答案。