在这些代码中,我可以获取所选项目,未选中的内容如何?
<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;
}
答案 0 :(得分:0)
您可以使用长度属性:
foo.selectedIndex.length
答案 1 :(得分:0)
获取<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 demo: http://jsfiddle.net/davidThomas/dxkhtdkn/1/中说明了一个简单的用例。
参考文献: