[英]How to count the unselected items in list
In these code I can get the selected items, how about the unselected? 在这些代码中,我可以获得选定的项目,未选定的如何?
<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
One way to get the number of unselected <option>
items from a <select>
element: 从<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;
Without more idea of what you're doing, though, I have to leave it to you to implement that in your function; 但是,如果不了解您在做什么,我就不得不把它留给您来在您的函数中实现。 however a simple use-case is illustrated in this JS Fiddle demo: http://jsfiddle.net/davidThomas/dxkhtdkn/1/ . 但是,此JS Fiddle演示中说明了一个简单的用例:http://jsfiddle.net/davidThomas/dxkhtdkn/1/ 。
References: 参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.