var url = 'addUser.action';
var params = {
companyid:-1
};
$("#company").empty();
jQuery.post(url, params, createCompany, 'json');
});
function createCompany(data){
var a = data.companylist;
var company = document.getElementById('company');
company.options.length=0;
$("<option value='-1'>--请选择--</option>").appendTo($("#company"));
for(var i=0;i<a.length;i++){
var option = new Option(a[i].companyname, a[i].companyid);
try{
company.add(option);
}catch(e){
}
}
}
......
<body style="margin-top:0; margin-left:0; margin-right:0; margin-bottom:0; width:100%;height:100%;">
<s:iterator value="#request.users" var="users">
<s:form action="updateUser.action" method="post" enctype ="multipart/form-data">
公司:
<select id="company" name="company" onChange="changecompany()" style="width:100px;">
<option value="-1">--请选择--</option>
</select>
姓名:
<s:textfield name="#users.username" theme="simple"></s:textfield>
......
数据在打开页面的时候jquery加载,这里是修改页面,那如何根据得到的公司值#users.companyid让公司下拉框默认选中某个公司?
比如下拉框已经显示了多个公司,如A,B,C公司等,现在是修改页面,比如用户是B公司,那么让B公司默认选中
公司值是#users.companyid,struts2框架
7 个解决方案
#1
换成<s:select>标签
#2
像你这种操作,感觉没有必要用jquery的ajax来实现的。
你只需要用<s:select的标签,来非常轻松的实现。后台是那个值,会自动选择的。
你只需要用<s:select的标签,来非常轻松的实现。后台是那个值,会自动选择的。
#3
公司改变的时候,部门如何跟着变啊?
<td>
公司:
</td>
<td>
<s:select list="#request.companylist" listKey="companyid" listValue="companyname"
value="#session.companyid" headerKey="0" headerValue="--请选择--" theme="simple" onchange="changecompany()"></s:select>
</td>
</tr>
<tr style="background-color:#ffffff;">
<td>
部门:
</td>
<td>
<s:select list="#request.departmentlist" listKey="departmentid" listValue="departmentname"
value="#session.departmentid" headerKey="0" headerValue="--请选择--" theme="simple"></s:select>
</td>
-------------------------------------------
public String getCompany2() throws Exception {
String companyid = ActionContext.getContext().getSession().get("companyid").toString();
String departmentid = ActionContext.getContext().getSession().get("departmentid").toString();
db1=new DBControl();
String SQL = "SELECT * FROM company";
ResultSet rs = null;
rs = db1.executeQuery(SQL);
ArrayList<addUser> companylist = new ArrayList<addUser>();
while (rs.next()) {
addUser u1 = new addUser(rs.getString("companyid"),rs.getString("companyname"),"","","","");
companylist.add(u1);
}
setCompanylist(companylist);
SQL = "SELECT * FROM department where companyid='"+companyid+"'";
rs = db1.executeQuery(SQL);
ArrayList<addUser> departmentlist = new ArrayList<addUser>();
while (rs.next()) {
String dep = URLDecoder.decode(rs.getString("departmentname"),"UTF-8");
addUser u1 = new addUser("","",rs.getString("departmentid"),dep,"","");
departmentlist.add(u1);
}
request.setAttribute("companylist", companylist);
request.setAttribute("departmentlist", departmentlist);
return "success";
}
<td>
公司:
</td>
<td>
<s:select list="#request.companylist" listKey="companyid" listValue="companyname"
value="#session.companyid" headerKey="0" headerValue="--请选择--" theme="simple" onchange="changecompany()"></s:select>
</td>
</tr>
<tr style="background-color:#ffffff;">
<td>
部门:
</td>
<td>
<s:select list="#request.departmentlist" listKey="departmentid" listValue="departmentname"
value="#session.departmentid" headerKey="0" headerValue="--请选择--" theme="simple"></s:select>
</td>
-------------------------------------------
public String getCompany2() throws Exception {
String companyid = ActionContext.getContext().getSession().get("companyid").toString();
String departmentid = ActionContext.getContext().getSession().get("departmentid").toString();
db1=new DBControl();
String SQL = "SELECT * FROM company";
ResultSet rs = null;
rs = db1.executeQuery(SQL);
ArrayList<addUser> companylist = new ArrayList<addUser>();
while (rs.next()) {
addUser u1 = new addUser(rs.getString("companyid"),rs.getString("companyname"),"","","","");
companylist.add(u1);
}
setCompanylist(companylist);
SQL = "SELECT * FROM department where companyid='"+companyid+"'";
rs = db1.executeQuery(SQL);
ArrayList<addUser> departmentlist = new ArrayList<addUser>();
while (rs.next()) {
String dep = URLDecoder.decode(rs.getString("departmentname"),"UTF-8");
addUser u1 = new addUser("","",rs.getString("departmentid"),dep,"","");
departmentlist.add(u1);
}
request.setAttribute("companylist", companylist);
request.setAttribute("departmentlist", departmentlist);
return "success";
}
#4
啊?没人帮我啊
#5
写一个onclick函数,公司下拉框值改变的时候触发,js里面获取id,name之类的,再作相应处理,改变部门,要查数据库的话这边可以用ajax
#6
楼主可以采纳二楼三楼的方法的,用struts2的select 标签,把公司id 和名称封装成个map 传递过去
#7
<select id="pgameCode" name="pgameCode">
<option value="0">
--全部--
</option>
<c:forEach items="${commpyList}" var="gb">
<option value="${gb.id }" ${vo.id==gb.id?'selected':''}>
${gb.gameName }
</option>
</c:forEach>
</select>
在加载select的时候就判断,如果当前循环的公司id等于你修改公司的id则给它默认选中,当然你必须得把你修改的公司的id传回页面。
<option value="0">
--全部--
</option>
<c:forEach items="${commpyList}" var="gb">
<option value="${gb.id }" ${vo.id==gb.id?'selected':''}>
${gb.gameName }
</option>
</c:forEach>
</select>
在加载select的时候就判断,如果当前循环的公司id等于你修改公司的id则给它默认选中,当然你必须得把你修改的公司的id传回页面。
#1
换成<s:select>标签
#2
像你这种操作,感觉没有必要用jquery的ajax来实现的。
你只需要用<s:select的标签,来非常轻松的实现。后台是那个值,会自动选择的。
你只需要用<s:select的标签,来非常轻松的实现。后台是那个值,会自动选择的。
#3
公司改变的时候,部门如何跟着变啊?
<td>
公司:
</td>
<td>
<s:select list="#request.companylist" listKey="companyid" listValue="companyname"
value="#session.companyid" headerKey="0" headerValue="--请选择--" theme="simple" onchange="changecompany()"></s:select>
</td>
</tr>
<tr style="background-color:#ffffff;">
<td>
部门:
</td>
<td>
<s:select list="#request.departmentlist" listKey="departmentid" listValue="departmentname"
value="#session.departmentid" headerKey="0" headerValue="--请选择--" theme="simple"></s:select>
</td>
-------------------------------------------
public String getCompany2() throws Exception {
String companyid = ActionContext.getContext().getSession().get("companyid").toString();
String departmentid = ActionContext.getContext().getSession().get("departmentid").toString();
db1=new DBControl();
String SQL = "SELECT * FROM company";
ResultSet rs = null;
rs = db1.executeQuery(SQL);
ArrayList<addUser> companylist = new ArrayList<addUser>();
while (rs.next()) {
addUser u1 = new addUser(rs.getString("companyid"),rs.getString("companyname"),"","","","");
companylist.add(u1);
}
setCompanylist(companylist);
SQL = "SELECT * FROM department where companyid='"+companyid+"'";
rs = db1.executeQuery(SQL);
ArrayList<addUser> departmentlist = new ArrayList<addUser>();
while (rs.next()) {
String dep = URLDecoder.decode(rs.getString("departmentname"),"UTF-8");
addUser u1 = new addUser("","",rs.getString("departmentid"),dep,"","");
departmentlist.add(u1);
}
request.setAttribute("companylist", companylist);
request.setAttribute("departmentlist", departmentlist);
return "success";
}
<td>
公司:
</td>
<td>
<s:select list="#request.companylist" listKey="companyid" listValue="companyname"
value="#session.companyid" headerKey="0" headerValue="--请选择--" theme="simple" onchange="changecompany()"></s:select>
</td>
</tr>
<tr style="background-color:#ffffff;">
<td>
部门:
</td>
<td>
<s:select list="#request.departmentlist" listKey="departmentid" listValue="departmentname"
value="#session.departmentid" headerKey="0" headerValue="--请选择--" theme="simple"></s:select>
</td>
-------------------------------------------
public String getCompany2() throws Exception {
String companyid = ActionContext.getContext().getSession().get("companyid").toString();
String departmentid = ActionContext.getContext().getSession().get("departmentid").toString();
db1=new DBControl();
String SQL = "SELECT * FROM company";
ResultSet rs = null;
rs = db1.executeQuery(SQL);
ArrayList<addUser> companylist = new ArrayList<addUser>();
while (rs.next()) {
addUser u1 = new addUser(rs.getString("companyid"),rs.getString("companyname"),"","","","");
companylist.add(u1);
}
setCompanylist(companylist);
SQL = "SELECT * FROM department where companyid='"+companyid+"'";
rs = db1.executeQuery(SQL);
ArrayList<addUser> departmentlist = new ArrayList<addUser>();
while (rs.next()) {
String dep = URLDecoder.decode(rs.getString("departmentname"),"UTF-8");
addUser u1 = new addUser("","",rs.getString("departmentid"),dep,"","");
departmentlist.add(u1);
}
request.setAttribute("companylist", companylist);
request.setAttribute("departmentlist", departmentlist);
return "success";
}
#4
啊?没人帮我啊
#5
写一个onclick函数,公司下拉框值改变的时候触发,js里面获取id,name之类的,再作相应处理,改变部门,要查数据库的话这边可以用ajax
#6
楼主可以采纳二楼三楼的方法的,用struts2的select 标签,把公司id 和名称封装成个map 传递过去
#7
<select id="pgameCode" name="pgameCode">
<option value="0">
--全部--
</option>
<c:forEach items="${commpyList}" var="gb">
<option value="${gb.id }" ${vo.id==gb.id?'selected':''}>
${gb.gameName }
</option>
</c:forEach>
</select>
在加载select的时候就判断,如果当前循环的公司id等于你修改公司的id则给它默认选中,当然你必须得把你修改的公司的id传回页面。
<option value="0">
--全部--
</option>
<c:forEach items="${commpyList}" var="gb">
<option value="${gb.id }" ${vo.id==gb.id?'selected':''}>
${gb.gameName }
</option>
</c:forEach>
</select>
在加载select的时候就判断,如果当前循环的公司id等于你修改公司的id则给它默认选中,当然你必须得把你修改的公司的id传回页面。