首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何计算列表中未选定的项

如何计算列表中未选定的项
EN

Stack Overflow用户
提问于 2014-09-08 09:29:27
回答 2查看 79关注 0票数 0

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

代码语言:javascript
复制
 <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;
        }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-08 09:51:32

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

代码语言:javascript
复制
// 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/演示了一个简单的用例。

参考文献:

票数 0
EN

Stack Overflow用户

发布于 2014-09-08 09:30:24

您可以使用length属性:

代码语言:javascript
复制
foo.selectedIndex.length
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25721109

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档