郁闷死了,刚才文章快写完了,浏览器死了......
在写一遍吧,可怜的孩子
首先我们来看看html,记住要导入property
ok,这个html应该没有什么好解释的吧?
在来看看js
简单说一下这个js。
custPopSelect.shtml这个代表要调用的action。而这个parameters: ' p=pro_city&value= ' + value就是参数了
因为我使用的dispatchaction所以用了p这个参数来指向action。而value就是省份的值了.
在写一遍吧,可怜的孩子
首先我们来看看html,记住要导入property
<
tr
>
< td align ="right" > 所在省份 </ td >
< td >
< select name ="sfmc" id ="select3" class ="inputred" onchange ="pro_city(this.value,'szdq')" >
< option value ="" > --请选择-- </ option >
< MD:option code ="1010" tj ="parid=1010" kind ="parid" dvalue ="${param.mc }" />
</ select >
</ td >
< td >
所在地区 < select name ="szdq" id ="szdq" class ="inputred" >
< option value ="" > --请选择-- </ option >
</ select >
</ td >
</ tr >
< td align ="right" > 所在省份 </ td >
< td >
< select name ="sfmc" id ="select3" class ="inputred" onchange ="pro_city(this.value,'szdq')" >
< option value ="" > --请选择-- </ option >
< MD:option code ="1010" tj ="parid=1010" kind ="parid" dvalue ="${param.mc }" />
</ select >
</ td >
< td >
所在地区 < select name ="szdq" id ="szdq" class ="inputred" >
< option value ="" > --请选择-- </ option >
</ select >
</ td >
</ tr >
在来看看js
//
省份城市联动
// value 省份的值
// con 需要填充的select 这里是城市
pro_city = function (value,con){
var showResponse = function (transport){
var json = eval(transport.responseText);
$(con).options.length = 0 ;
if (json == null || json == undefined){
var oNewNode = new Option();
oNewNode.value = "" ;
oNewNode.text = " --请选择-- " ;
$(con).options.add(oNewNode);
}
else {
for ( var i = 0 ;i < json.length;i ++ ){
var oNewNode = new Option();
oNewNode.value = json[i].key;
oNewNode.text = json[i].value;
$(con).options.add(oNewNode);
}
}
}
new Ajax.Request( ' /custPopSelect.shtml ' ,
{
method: ' post ' ,
parameters: ' p=pro_city&value= ' + value,
onComplete: showResponse
}
);
}
// value 省份的值
// con 需要填充的select 这里是城市
pro_city = function (value,con){
var showResponse = function (transport){
var json = eval(transport.responseText);
$(con).options.length = 0 ;
if (json == null || json == undefined){
var oNewNode = new Option();
oNewNode.value = "" ;
oNewNode.text = " --请选择-- " ;
$(con).options.add(oNewNode);
}
else {
for ( var i = 0 ;i < json.length;i ++ ){
var oNewNode = new Option();
oNewNode.value = json[i].key;
oNewNode.text = json[i].value;
$(con).options.add(oNewNode);
}
}
}
new Ajax.Request( ' /custPopSelect.shtml ' ,
{
method: ' post ' ,
parameters: ' p=pro_city&value= ' + value,
onComplete: showResponse
}
);
}
custPopSelect.shtml这个代表要调用的action。而这个parameters: ' p=pro_city&value= ' + value就是参数了
因为我使用的dispatchaction所以用了p这个参数来指向action。而value就是省份的值了.
/**
* 省份城市联动
*
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward pro_city(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String value = request.getParameter( " value " );
if ( ! "" .equals(value)) {
String json = "" ;
List < KeyValue > kvList = new ArrayList < KeyValue > ();
B_class_service b_class_service = (B_class_service) WebAppUtil
.getService( " b_class_service " , request);
B_class b_class = new B_class();
b_class.setParid(value);
List < B_class > list = b_class_service.getB_classAll(b_class);
for (B_class b : list) {
KeyValue k = new KeyValue();
k.setKey(b.getId());
k.setValue(b.getMc());
kvList.add(k);
}
json = UtilComp.Iterator2Json(kvList.iterator(), new String[] {
" key " , " value " });
UtilComp.renderText(response, json);
}
return null ;
}
* 省份城市联动
*
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward pro_city(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String value = request.getParameter( " value " );
if ( ! "" .equals(value)) {
String json = "" ;
List < KeyValue > kvList = new ArrayList < KeyValue > ();
B_class_service b_class_service = (B_class_service) WebAppUtil
.getService( " b_class_service " , request);
B_class b_class = new B_class();
b_class.setParid(value);
List < B_class > list = b_class_service.getB_classAll(b_class);
for (B_class b : list) {
KeyValue k = new KeyValue();
k.setKey(b.getId());
k.setValue(b.getMc());
kvList.add(k);
}
json = UtilComp.Iterator2Json(kvList.iterator(), new String[] {
" key " , " value " });
UtilComp.renderText(response, json);
}
return null ;
}
这里我用了KeyValue这个javabean,得到一个城市的对象集合,把城市的id和value同事放到KeyValue的key和value里面,这个javabean只有两个参数.
等等好像还差点什么,这里需要一个工具类
/**
* 指定fields包含的属性
*
* @param it
* @param fields
* @return
*/
public static String Iterator2Json(Iterator it, String[] fields) {
String bs = null ;
String rnt = null ;
if (it != null ) {
while (it.hasNext()) {
Object bean = it.next();
bs = Bean2Json(bean, fields);
if (rnt == null ) {
rnt = bs;
} else {
rnt = rnt + " , " + " " + bs;
}
}
}
if (rnt != null ) {
rnt = " [ " + " " + rnt + " " + " ] " ;
}
if (rnt == null || "" .equals(rnt)) {
rnt = " [] " ;
}
return rnt;
}
* 指定fields包含的属性
*
* @param it
* @param fields
* @return
*/
public static String Iterator2Json(Iterator it, String[] fields) {
String bs = null ;
String rnt = null ;
if (it != null ) {
while (it.hasNext()) {
Object bean = it.next();
bs = Bean2Json(bean, fields);
if (rnt == null ) {
rnt = bs;
} else {
rnt = rnt + " , " + " " + bs;
}
}
}
if (rnt != null ) {
rnt = " [ " + " " + rnt + " " + " ] " ;
}
if (rnt == null || "" .equals(rnt)) {
rnt = " [] " ;
}
return rnt;
}
ok,到这里就和大家说再见了