❶ 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+"被选中了");
}
}
}