Ⅰ 使用jquery實現下拉多選需要哪些js和css
jquery 獲取select多選下拉框所有選項的值可以如下實現
var all = "";
$("select option").each(function() {
all += $(this).attr("value")+" ";
});
而值獲取被選中的值可用如下代碼實現
$("select").val();
下面給出實例代碼:
創建Html元素:一個多選列表和一個按鈕
<select id="multi-sel" multiple="multiple">
<option value="1">蘿卜,我的value是1</option>
<option value="2">青菜,我的value是2</option>
<option value="3">小蔥,我的value是3</option>
<option value="4">豆腐,我的value是4</option>
</select>
<input type="button" value="點擊顯示選擇的項目">
簡單設置一下css樣式
select{width:200px;height:150px;padding:10px;border:4px dashed #ccc;}
select option{margin:5px;}
input[type='button']{width:200px;height:35px;margin:10px;border:2px dashed #ebbcbe;}
編寫jquery代碼:實現獲取所有值和獲取選中值
$(function(){
$("input:button").click(function() {
var all = "";
$("select option").each(function() {
all += $(this).attr("value")+" ";
});
var sel = $("select").val();
alert("多選列表所有的value值:"+all+",其中被選中的是:"+sel+"。");
});
})
觀察效果
Ⅱ 怎樣實現asp下拉菜單多選
把這一句 <select name="categoryid">
改成 <select name="categoryid" multiple>
試試, multiple 的作用是 可選多個選項
選多個選項時要按住ctrl鍵再單擊選項
Ⅲ Extjs怎麼實現下拉框多選
|1、擴展js類庫,在項目中建立一個 js文件,命名為:xxx.js 其代碼為
if('function'!==typeofRegExp.escape)
{
RegExp.escape=function(s)
{
if('string'!==typeofs)
{
returns;
}
returns.replace(/([.*+?^=!:${}()|[]/\])/g,'\$1');
};
}
Ext.ns('Ext.form');
Ext.form.MultiSelect=Ext.extend(Ext.form.ComboBox,
{
checkField:'checked',
multi:true,
separator:',',
initComponent:function()
{
if(!this.tpl)
{
this.tpl='<tplfor=".">'+'<divclass="x-combo-list-item">'
+'<imgsrc="'+Ext.BLANK_IMAGE_URL+'"'
+'class="ux-MultiSelect-iconux-MultiSelect-icon-'
+'{[values.'+this.checkField+'?"checked":"unchecked"'
+']}">'
+'{[values.'+this.displayField+']}'
+'</div>'
+'</tpl>';
}
Ext.form.MultiSelect.superclass.initComponent.apply(this,arguments);
this.on(
{
scope:this,
beforequery:this.onBeforeQuery,
blur:this.onRealBlur
});
this.onLoad=this.onLoad.createSequence(function()
{
if(this.el)
{
varv=this.el.dom.value;
this.el.dom.value='';
this.el.dom.value=v;
}
});
},
initEvents:function()
{
Ext.form.MultiSelect.superclass.initEvents.apply(this,arguments);
this.keyNav.tab=false;
},
beforeBlur:function()
{
},
postBlur:function()
{
},
clearValue:function()
{
this.value='';
this.setRawValue(this.value);
this.store.clearFilter();
this.store.each(function(r)
{
r.set(this.checkField,false);
},this);
if(this.hiddenField)
{
this.hiddenField.value='';
}
this.applyEmptyText();
},
getCheckedDisplay:function()
{
varre=newRegExp(this.separator,"g");
returnthis.getCheckedValue(this.displayField).replace(re,this.separator+'');
},
getCheckedValue:function(field)
{
field=field||this.valueField;
varc=[];
varsnapshot=this.store.snapshot||this.store.data;
snapshot.each(function(r)
{
if(r.get(this.checkField))
{
c.push(r.get(field));
}
},this);
returnc.join(this.separator);
},
onBeforeQuery:function(qe)
{
qe.query=qe.query.replace(newRegExp(RegExp.escape(this.getCheckedDisplay())+'['+this.separator+']*'),'');
},
onRealBlur:function()
{
this.list.hide();
varrv=this.getRawValue();
varrva=rv.split(newRegExp(RegExp.escape(this.separator)+'*'));
varva=[];
varsnapshot=this.store.snapshot||this.store.data;
Ext.each(rva,function(v)
{
snapshot.each(function(r)
{
if(v===r.get(this.displayField))
{
va.push(r.get(this.valueField));
}
},this);
},this);
this.setValue(va.join(this.separator));
this.store.clearFilter();
},
onSelect:function(record,index)
{
if(this.fireEvent('beforeselect',this,record,index)!==false)
{
record.set(this.checkField,!record.get(this.checkField));
if(this.store.isFiltered())
{
this.doQuery(this.allQuery);
}
if(this.multi)
{
if(record.get("key")=="---"&&record.get(this.checkField))
{
this.setValue("---");
}
else
{
this.setValue(this.getCheckedValue());
}
}
else
{
this.clearValue();
this.value=record.get(this.valueField);
this.setRawValue(record.get(this.displayField));
this.list.hide();
}
this.fireEvent('select',this,record,index);
}
},
setValue:function(v)
{
if(v)
{
v=''+v;
if(this.valueField)
{
this.store.clearFilter();
this.store.each(function(r)
{
varchecked=!(!v.match('(^|'+this.separator+')'
+RegExp.escape(r.get(this.valueField))
+'('+this.separator+'|$)'));
r.set(this.checkField,checked);
},this);
this.value=this.getCheckedValue();
this.setRawValue(this.getCheckedDisplay());
if(this.hiddenField)
{
this.hiddenField.value=this.value;
}
}
else
{
this.value=v;
this.setRawValue(v);
if(this.hiddenField)
{
this.hiddenField.value=v;
}
}
if(this.el)
{
this.el.removeClass(this.emptyClass);
}
}
else
{
this.clearValue();
}
},
selectAll:function()
{
this.store.each(function(record)
{
record.set(this.checkField,true);
},this);
this.doQuery(this.allQuery);
this.setValue(this.getCheckedValue());
},
deselectAll:function()
{
this.clearValue();
}
});
Ext.reg('multiSelect',Ext.form.MultiSelect);
2、在ext-all.css文件最後,加入css樣式
.ux-MultiSelect-icon{width:16px;height:16px;float:left;background-position:-1px-1px!important;background-repeat:no-repeat!important;}
.ux-MultiSelect-icon-checked{background:transparenturl(../images/default/menu/checked.gif);}
.ux-MultiSelect-icon-unchecked{background:transparenturl(../images/default/menu/unchecked.gif);}
3、使用
varDepartUserStore=newExt.data.Store(
{
proxy:newExt.data.HttpProxy(
{
url:'/Web/Manage/DeskTop/JSON/ScheleManager/GetSimpleDepartUserInfo.aspx'
}),
//讀取Json
reader:newExt.data.JsonReader(
{totalProperty:"totalCount",root:"root"},
[
{name:'UserId',type:'int'},
{name:'UserName',type:'string'}
])
});
varDepartUserCbox=newExt.form.MultiSelect(
{
fieldLabel:'姓名',
labelStyle:'width:80px',
width:150,
editable:false,
id:'DepartUserDS',
hiddenName:'DepartUserIdDS',
store:DepartUserStore,
emptyText:'--請選擇--',
allowBlank:false,
blankText:'請選擇',
mode:'remote',
displayField:'UserName',
valueField:'UserId',
triggerAction:'all',
selectOnFocus:true,
listWidth:200
});
DepartUserStore.on('load',function()
{
DepartUserCbox.selectAll();//全選
});
DepartUserStore.load();
Ⅳ WPS excel中如何設置下拉菜單,使得可以多選數據。
1、打開表格然後先中需要設置下拉的單元格。
Ⅳ js怎麼能取得多選下拉框選中的多個值
方法:獲取多選下拉框對象數組→循環判斷option選項的selected屬性(true為選中,false為未選中)→使用value屬性取出選中項的值。實例演示如下:
1、HTML結構
<selectid="test"multiple="true">
<optionvalue="option-A">option-A</option>
<optionvalue="option-B">option-B</option>
<optionvalue="option-C">option-C</option>
<optionvalue="option-D">option-D</option>
</select>
<inputtype="button"value="確定"onclick="fun()"/>
2、javascript代碼
functionfun(){
varselect=document.getElementById("test");
varstr=[];
for(i=0;i<select.length;i++){
if(select.options[i].selected){
str.push(select[i].value);
}
}
alert(str);
}
3、效果演示