繁体   English   中英

如何计算列表中未选择的项目

[英]How to count the unselected items in list

在这些代码中,我可以获得选定的项目,未选定的如何?

 <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;
        }

您可以使用length属性:

foo.selectedIndex.length

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

// 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;

但是,如果不了解您在做什么,我就不得不把它留给您来在您的函数中实现。 但是,此JS Fiddle演示中说明了一个简单的用例:http://jsfiddle.net/davidThomas/dxkhtdkn/1/

参考文献:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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