在这些代码中,我可以获得所选的项,未选择的项如何?
<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;
}发布于 2014-09-08 09:51:32
从一个<option>元素中获取未选择的<select>项数的一种方法:
// 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/演示了一个简单的用例。
参考文献:
发布于 2014-09-08 09:30:24
您可以使用length属性:
foo.selectedIndex.lengthhttps://stackoverflow.com/questions/25721109
复制相似问题