Inb*_*nds 5 javascript jquery html-select
我有一个通用的下拉列表填充脚本,它填充select从各种jquery调用返回的选项.它目前仅供单一选择.我需要添加填充多选的功能,它按原样工作,但我不想包含初始*请选择*选项.我正在寻找一个jQuery或简单的Javascript解决方案.
if (dropdown != null) {
var regList = document.getElementById(dropdown);
regList.options.length = 0;
var opt = document.createElement("option");
// ** if the dropdown is *not* a multiple="multiple",
// add the first option as an empty value
opt.text = " -- Select --";
opt.value = "";
regList.options.add(opt);
// **
// loop thorugh child nodes and fill dropdown.
$.each(arg.d, function () {
opt = document.createElement("option");
opt.text = this;
opt.value = this;
regList.options.add(opt);
});
$('.SpinImage').remove();
}
Run Code Online (Sandbox Code Playgroud)
smc*_*mcd 10
您可以检查对象的.multiple属性
var isMulti = document.getElementById('selectId').multiple; // true/false
Run Code Online (Sandbox Code Playgroud)
jQuery你可以做.prop('multiple');
$('#selectId').prop('multiple');
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/31eLyzb3/4/
/*
Using jQuery, finds and populates the selects on the page.
*/
(function populateAll() {
/* Here's our options info */
var opts = {
'volvo': 'Volvo',
'saab' : 'Saab',
'opel' : 'Opel',
'audi' : 'Audi'
};
/*
This function, given a jQuery select item, and options list, populates it.
If it is a single-select, it adds a ---Select---/empty value as the first option.
*/
function populate(listBox, options) {
var option = null;
if (listBox.prop('multiple') === false) {
option = document.createElement('option');
option.text = '---Select---';
option.value = '';
listBox.append(option);
}
$.each(options, function (value, label) {
option = document.createElement('option');
option.text = label;
option.value = value;
listBox.append(option);
});
}
/* Find all select tags on page and populate with 'opts' data */
$('select').each(function() {
populate($(this), opts);
});
})();
Run Code Online (Sandbox Code Playgroud)