JSP表单select多级联动问题
2014-03-24 15:34
239 查看
注意:每次都要定义branch1,branch2,branch3,这样避免变量全局局部的问题
<tr>
<td>一级部门:</td>
<td><selectonchange="getbranch1()" id="branch1"name="branch1_no"></select></td>
<td>二级部门:</td>
<td><selectonchange="getbranch2()" id="branch2"name="branch2_no"></select></td>
<td>三级部门:</td>
<td><selectid="branch3" name="branch3_no"></select></td>
</tr>
Branch1,branch2,branch3这三级联动,
方式有两种,一种是ajax一级,一级选择->ajax二级,二级选择->ajax三级,三级选择,这样需要ajax传递三次数据到后台
另一种是,ajax一级,二级,三级数据到jsp,然后js函数控制联动筛选显示,本项目采用的是第二种,速度快,更实用。
(1)ajax拿到所有数据
functionbranchlist(){
$.ajax({
type:"POST",
url:"branchAction.action",
dataType:"json",
data:"",
success: function(json){
list1 = json.branch1list;
list2 = json.branch2list;
var branch1=$("#branch1")[0];
branch1.options.length=0;
branch1.options.add(new Option("所有一级部门","0"));
var branch2=$("#branch2")[0];
branch2.options.length=0;
branch2.options.add(new Option("所有二级部门","00"));
var branch3=$("#branch3")[0];
branch3.options.length=0;
branch3.options.add(new Option("所有三级部门","000"));
for(var i=0;i<list1.length;i++){
var option= newOption(list1[i].branch,list1[i].branch_no);
branch1.options.add(option);
}
selRefresh("branch1");
selRefresh("branch2");
selRefresh("branch3"); }
})
}
(2)js函数判断联动
functiongetbranch1(){
//根据选择的branch1的值,branch2需要载入的数据联动变化
var branch1=$("#branch1")[0];
var branch2=$("#branch2")[0];
var branch3=$("#branch3")[0];
document.all.branch2.options.length =1;
document.all.branch3.options.length =1;
var firstno=branch1.options[branch1.options.selectedIndex].value;
if(firstno != "000")
{
for(vari=0;i<list2.length;i++){
var funo =(list2[i].branch_no).substring(0,3);
if(funo = firstno)
{
var option= newOption(list2[i].branch,list2[i].branch_no);
branch2.options.add(option);
}
}
}
else{
document.all.branch2.options.length= 1;
document.all.branch3.options.length= 1;
}
selRefresh("branch2");
selRefresh("branch3");
}
functiongetbranch2(){
//根据选择的branch2的值,branch3需要载入的数据联动变化
var branch2=$("#branch2")[0];
var branch3=$("#branch3")[0];
document.all.branch3.options.length =1;
var secondno= branch2.options[branch2.options.selectedIndex].value;
if(secondno != "000")
{
for(var i=0;i<list3.length;i++){
var funo =(list3[i].branch_no).substring(0,6);
if(funo == secondno)
{
var option= newOption(list3[i].branch,list3[i].branch_no);
branch3.options.add(option);
}
}
}
else{
document.all.branch3.options.length= 1;
}
selRefresh("branch3");
}
ajax传递参数的方式:
$.ajax({
type: "POST",
url: "<%=path%>/xxxAction.action",
dataType:"json",
data:"startIndex="+startIndex+"&endIndex="+endIndex+"&branchno="+branchno+"&flag="+flag,
<tr>
<td>一级部门:</td>
<td><selectonchange="getbranch1()" id="branch1"name="branch1_no"></select></td>
<td>二级部门:</td>
<td><selectonchange="getbranch2()" id="branch2"name="branch2_no"></select></td>
<td>三级部门:</td>
<td><selectid="branch3" name="branch3_no"></select></td>
</tr>
Branch1,branch2,branch3这三级联动,
方式有两种,一种是ajax一级,一级选择->ajax二级,二级选择->ajax三级,三级选择,这样需要ajax传递三次数据到后台
另一种是,ajax一级,二级,三级数据到jsp,然后js函数控制联动筛选显示,本项目采用的是第二种,速度快,更实用。
(1)ajax拿到所有数据
functionbranchlist(){
$.ajax({
type:"POST",
url:"branchAction.action",
dataType:"json",
data:"",
success: function(json){
list1 = json.branch1list;
list2 = json.branch2list;
var branch1=$("#branch1")[0];
branch1.options.length=0;
branch1.options.add(new Option("所有一级部门","0"));
var branch2=$("#branch2")[0];
branch2.options.length=0;
branch2.options.add(new Option("所有二级部门","00"));
var branch3=$("#branch3")[0];
branch3.options.length=0;
branch3.options.add(new Option("所有三级部门","000"));
for(var i=0;i<list1.length;i++){
var option= newOption(list1[i].branch,list1[i].branch_no);
branch1.options.add(option);
}
selRefresh("branch1");
selRefresh("branch2");
selRefresh("branch3"); }
})
}
(2)js函数判断联动
functiongetbranch1(){
//根据选择的branch1的值,branch2需要载入的数据联动变化
var branch1=$("#branch1")[0];
var branch2=$("#branch2")[0];
var branch3=$("#branch3")[0];
document.all.branch2.options.length =1;
document.all.branch3.options.length =1;
var firstno=branch1.options[branch1.options.selectedIndex].value;
if(firstno != "000")
{
for(vari=0;i<list2.length;i++){
var funo =(list2[i].branch_no).substring(0,3);
if(funo = firstno)
{
var option= newOption(list2[i].branch,list2[i].branch_no);
branch2.options.add(option);
}
}
}
else{
document.all.branch2.options.length= 1;
document.all.branch3.options.length= 1;
}
selRefresh("branch2");
selRefresh("branch3");
}
functiongetbranch2(){
//根据选择的branch2的值,branch3需要载入的数据联动变化
var branch2=$("#branch2")[0];
var branch3=$("#branch3")[0];
document.all.branch3.options.length =1;
var secondno= branch2.options[branch2.options.selectedIndex].value;
if(secondno != "000")
{
for(var i=0;i<list3.length;i++){
var funo =(list3[i].branch_no).substring(0,6);
if(funo == secondno)
{
var option= newOption(list3[i].branch,list3[i].branch_no);
branch3.options.add(option);
}
}
}
else{
document.all.branch3.options.length= 1;
}
selRefresh("branch3");
}
ajax传递参数的方式:
$.ajax({
type: "POST",
url: "<%=path%>/xxxAction.action",
dataType:"json",
data:"startIndex="+startIndex+"&endIndex="+endIndex+"&branchno="+branchno+"&flag="+flag,
相关文章推荐
- JSP表单提交中文编码问题解决方法
- html select 不可编辑时表单提交问题
- 用ajax、JSP和Servlet实现多级下拉菜单无刷新联动
- 表单元素radio select对齐与IE6下双边距问题解决方案
- JavaScript 多级联动select
- 「The Most Important」浅谈JSP表单Post方式中文乱码问题
- 解决jsp表单重复提交问题
- jsp小问题之 表单传送中文信息
- 在jsp中使用struts表单标签时出现找不到路径的问题
- jQuery cxSelect 多级联动下拉菜单
- 使用struts2提交jsp页面表单时参数中汉字乱码的问题
- jsp提交表单乱码问题
- 公众号门店开发,实现select表单省市县三级联动2
- JavaScript 多级联动select
- jsp传递中文参数、空格以及表单内容换行问题解决小结
- 关于Dropdownlist 与 autopostBack 问题-多级联动 例 省/市/区
- layer表单元素 三级联动 省市县选择框动态渲染问题解决
- jsp传递中文参数、空格以及表单内容换行问题解决小结
- [译]在启用浏览器功能的INFOPATH表单中实现基于SQL SERVER的多级联动的下拉式列表
- 研究了三天的文件上传(jspsmart实现)file和text表单同时提交的问题