❶ 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();
❷ jsp 如何獲取多選下拉列表框的值
多選框取一個相同的名字 比如叫name 然後在servlet里通過request.getParameterValues("name")得到一個String類型的數組。這個數專組就是屬多選框的值的數組
下拉列表也是取一個名字 然後request.getParameter("name") 得到一個String值
❸ jsp 如何伺服器端獲取多選下拉列表框的值
根據問題描述,應該你的意思是在伺服器端,把前台頁面的列表框選取的內容獲得到
例如在前台是這樣一個HTML代碼
-------------------------index.jsp----------------------------------------
<body>
<form action="result.jsp" method="get" name="form1">
<SELECT ID="oSelect" NAME="Cars" SIZE="3" MULTIPLE>
<OPTION VALUE="寶馬" SELECTED>寶馬
<OPTION VALUE="保時捷">保時捷
<OPTION VALUE="賓士" SELECTED>賓士
</SELECT>
<input type="submit" value="提交" />
</form>
</body>
------------------result.jsp----------------------
<body>
<%
request.setCharacterEncoding("utf-8");
String[] values = request.getParameterValues("Cars");
for (int i = 0; i < values.length; i++) {
values[i] = new String(values[i].getBytes("ISO-8859-1"), "utf-8");
%>
<input type="text" value="<%=values[i] %>" /><br/>
<%
}
%>
</body>
補充一下:這里特地用了get的方式而不是post,你如果自己試一下就可以看到傳過去的url是這樣的:
http://localhost:8080/test/result.jsp?Cars=%E5%AE%9D%E9%A9%AC&Cars=%E5%A5%94%E9%A9%B0
其中的亂碼部分,如果是用的Chrome之類可以看到中文參數的瀏覽器的話,其實地址是這樣的:
http://localhost:8080/test/result.jsp?Cars=寶馬&Cars=賓士
❹ jsp頁面中select標簽中怎麼加checkbox實現多選
使用JSP頁面代碼:
代碼如下:
<table>
<tr>
<td width="400px" align="left">入學批次:<SELECT NAME="grade"
id="grade" onchange="refreshELevelAndSpecialAjax();"> //選擇入學批次會刷新層次和專業
<OPTION VALUE="0">
--請選擇--
<c:forEach items="${gradeInfo}" var="gradeInfo">
<OPTION VALUE="${gradeInfo.gradeName}">${gradeInfo.gradeName}
</c:forEach>
</SELECT></td>
<td width="400px" align="left">統考課程:<SELECT
NAME="uniExamCourseId" id="uniExamCourseId">
<OPTION VALUE="0">
--請選擇--
<c:forEach items="${unifiedExamCourseList}" var="uniExamCourse">
<OPTION VALUE="${uniExamCourse.id}">${uniExamCourse.uniExamCourseName}
</c:forEach>
</SELECT></td>
</tr>
<tr>
<td colspan="2" id="refreshELevelAndSpecialAjax"> //設置ID,用於填充層次和專業的下拉框
<table>
<tr>
<td width="400" align="left">層 次:<SELECT
NAME="eLevelId" id="eLevelId"
onchange="refreshSpecialAjax();"> //選擇層次後刷新專業
<OPTION VALUE="0">--請選擇--</OPTION>
<c:forEach items="${ecationLevel}" var="ecationLevel">
<OPTION VALUE="${ecationLevel.id}">${ecationLevel.ecationLevelName}
</c:forEach>
</SELECT></td>
<td width="400" align="left" id="refreshSpecialAjax">專 業:<SELECT //設置ID,用於填充專業的下拉框
NAME="specialId" id="specialId">
<OPTION VALUE="0">--請選擇--</OPTION>
<c:forEach items="${specialList}" var="special">
<OPTION VALUE="${special.id}">${special.specialName}
</c:forEach>
</SELECT></td>
</tr>
</table>
</td>
</tr>
</table>
❺ 請高手給一個JS多文件上傳的例子(必須兼容IE)解決追加50分。請看補充。
一、Servlet實現文件上傳,需要添加第三方提供的jar包
下載地址:
1) commons-fileupload-1.2.2-bin.zip: 點擊打開鏈接
2) commons-io-2.3-bin.zip: 點擊打開鏈接
接著把這兩個jar包放到 lib文件夾下:
二:文件上傳的表單提交方式必須是POST方式,
編碼類型:enctype="multipart/form-data",默認是 application/x-www-form-urlencoded
比如:
<form action="FileUpLoad"enctype="multipart/form-data"method="post">
三、舉例:
1.fileupload.jsp
<%@ page language="java" import="javautil*" pageEncoding="UTF-8"%>
<%
String path = requestgetContextPath();
String basePath = requestgetScheme()+"://"+requestgetServerName()+":"+requestgetServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'fileuploadjsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="stylescss">
-->
</head>
<body>
<!-- enctype 默認是 application/x-www-form-urlencoded -->
<form action="FileUpLoad" enctype="multipart/form-data" method="post" >
用戶名:<input type="text" name="usename"> <br/>
上傳文件:<input type="file" name="file1"><br/>
上傳文件: <input type="file" name="file2"><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
2.實際處理文件上傳的 FileUpLoad.java
package comservletfileupload;
import javaioFile;
import javaio*;
import javaioIOException;
import javaioPrintWriter;
import javautilList;
import javaxservletServletException;
import javaxservlethttpHttpServlet;
import ;
import ;
import ;
import ;
import ;
import ;
/**
*
* @author Administrator
* 文件上傳
* 具體步驟:
* 1)獲得磁碟文件條目工廠 DiskFileItemFactory 要導包
* 2) 利用 request 獲取 真實路徑 ,供臨時文件存儲,和 最終文件存儲 ,這兩個存儲位置可不同,也可相同
* 3)對 DiskFileItemFactory 對象設置一些 屬性
* 4)高水平的API文件上傳處理 ServletFileUpload upload = new ServletFileUpload(factory);
* 目的是調用 parseRequest(request)方法 獲得 FileItem 集合list ,
*
* 5)在 FileItem 對象中 獲取信息, 遍歷, 判斷 表單提交過來的信息 是否是 普通文本信息 另做處理
* 6)
* 第一種 用第三方 提供的 itemwrite( new File(path,filename) ); 直接寫到磁碟上
* 第二種 手動處理
*
*/
public class FileUpLoad extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
requestsetCharacterEncoding("utf-8"); //設置編碼
//獲得磁碟文件條目工廠
DiskFileItemFactory factory = new DiskFileItemFactory();
//獲取文件需要上傳到的路徑
String path = requestgetRealPath("/upload");
//如果沒以下兩行設置的話,上傳大的 文件 會佔用 很多內存,
//設置暫時存放的 存儲室 , 這個存儲室,可以和 最終存儲文件 的目錄不同
/**
* 原理 它是先存到 暫時存儲室,然後在真正寫到 對應目錄的硬碟上,
* 按理來說 當上傳一個文件時,其實是上傳了兩份,第一個是以 tem 格式的
* 然後再將其真正寫到 對應目錄的硬碟上
*/
factorysetRepository(new File(path));
//設置 緩存的大小,當上傳文件的容量超過該緩存時,直接放到 暫時存儲室
factorysetSizeThreshold(1024*1024) ;
//高水平的API文件上傳處理
ServletFileUpload upload = new ServletFileUpload(factory);
try {
//可以上傳多個文件
List<FileItem> list = (List<FileItem>)uploadparseRequest(request);
for(FileItem item : list)
{
//獲取表單的屬性名字
String name = itemgetFieldName();
//如果獲取的 表單信息是普通的 文本 信息
if(itemisFormField())
{
//獲取用戶具體輸入的字元串 ,名字起得挺好,因為表單提交過來的是 字元串類型的
String value = itemgetString() ;
requestsetAttribute(name, value);
}
//對傳入的非 簡單的字元串進行處理 ,比如說二進制的 圖片,電影這些
else
{
/**
* 以下三步,主要獲取 上傳文件的名字
*/
//獲取路徑名
String value = itemgetName() ;
//索引到最後一個反斜杠
int start = valuelastIndexOf("\\");
//截取 上傳文件的 字元串名字,加1是 去掉反斜杠,
String filename = valuesubstring(start+1);
requestsetAttribute(name, filename);
//真正寫到磁碟上
//它拋出的異常 用exception 捕捉
//itemwrite( new File(path,filename) );//第三方提供的
//手動寫的
OutputStream out = new FileOutputStream(new File(path,filename));
InputStream in = itemgetInputStream() ;
int length = 0 ;
byte [] buf = new byte[1024] ;
Systemoutprintln("獲取上傳文件的總共的容量:"+itemgetSize());
// inread(buf) 每次讀到的數據存放在 buf 數組中
while( (length = inread(buf) ) != -1)
{
//在 buf 數組中 取出數據 寫到 (輸出流)磁碟上
outwrite(buf, 0, length);
}
inclose();
outclose();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
//eprintStackTrace();
}
requestgetRequestDispatcher("filedemojsp")forward(request, response);
}
}
System.out.println("獲取上傳文件的總共的容量:"+item.getSize());
3.filedemo.jsp
<%@ page language="java" import="javautil*" pageEncoding="UTF-8"%>
<%
String path = requestgetContextPath();
String basePath = requestgetScheme()+"://"+requestgetServerName()+":"+requestgetServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'filedemojsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="stylescss">
-->
</head>
<body>
用戶名:${requestScopeusename } <br/>
文件:${requestScopefile1 }<br/>
${requestScopefile2 }<br/>
<!-- 把上傳的圖片顯示出來 -->
<img alt="go" src="upload/<%=(String)requestgetAttribute("file1")%> " />
</body>
</html>
4結果頁面:
以上就是本文的全部
❻ 在jsp頁面中怎樣用javaScript檢查多選框是否被選中
var chk=document.getElementsByTagName("input");
for(var i=0,len=chk.length;i<len;i++){
if(chk[i].type=="checkbox"){
if(chk[i].checked){
alert(chk[i].id+"被選中了");
}
}
}
這樣就能得到所有的chkckbox了.
如果只是在某個范圍內,可以這樣
如
<div id="cc">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
</div>
則可以這樣得到div內的所有checkbox
var arr=document.getElementById("cc").getElementsByTagName("input");
for(var i=0,len=chk.length;i<len;i++){
if(chk[i].type=="checkbox"){
if(chk[i].checked){
alert(chk[i].id+"被選中了");
}
}
}
給你最外層的一個Table指定一個ID就可以了。如:給一個叫TblContainer的ID,可以這樣用。
var arr=document.getElementById("TblContainer").getElementsByTagName("input");
for(var i=0,len=chk.length;i<len;i++){
if(chk[i].type=="checkbox"){
if(chk[i].checked){
alert(chk[i].id+"被選中了");
}
}
}