我做过 一个物流网,里边有个要求到县的3级联动下拉框,后来找了很久找到一个60多K的js,不是很好,我改了一下,就凑合的着用了。
看《ajax in action》1个月了,想练练,又不知道从何入手,但是今天终于忍不住了。手攘攘的。
拿双组下拉框试试。结果2个小时搞定了,中间卡住了。
书上是第9章,例子是vb.net写的服务端,我要改成asp的。
动手了
我图方便,直接用重构好的组件,全都拷走,用dm8建一个DoubleComboXML.asp,把doubleCombo_refactored.htm改名为doubleCombo_refactored.asp,因为我第一个下拉框也要自动生成。
我要做的效果是:
从数据库调用省份放入第一个select,当省份onchange时,js向服务器查询选中的省份的所属市。
数据库这样设计:
prov表有id,desc ,city表有id,pid,desc
这样编程就不像以前一样集中了,要么全在客户端或服务端一次全部生成。
现在编程就分成了从服务端取出所有省份,查询一个省份的所有城市,还可以再加个查询这个城市的所有县。
无论从编程还是用户体验都是很棒的。
后来发现重构好的组件不工作,就又换成原始的,(不能被挫啊!),
在doubleCombo_refactored.asp(改成asp,为了运行asp代码)加了这个,
<!--#include file="conn.asp"-->
<%
dim sql : sql = "select id, desc from prov "
rs.open sql,conn,1,1
dim rstable : rstable=rs.getrows
rs.close
set rs=nothing
%>
改了这个:
<form id="form">
<select id="region" name="region" >
<option value="-1" selected>选择一个省份</option>
<%
for i=0 to ubound(rstable,2)
response.Write "<option value="""&rstable(0,i)&""">"&rstable(1,i)&"</option>"
next
%>
</select>
<select id="territory" name="territory" />
</form>
DoubleComboXML.asp(提交的url,改成这个,不是aspx的)
<!--#include file="conn.asp"-->
<%
dim strQuery,strForm,strElem,strSql,rstable,rowc
strQuery=cint(request("q"))
strForm=request("f")
strElem=request("e")
strSql="select "&_
"id, desc from city "&_
"where "&_
"pid="&strQuery
rs.open strSql,conn,1,1
rstable=rs.getrows
rs.close
set rs=nothing
rowc=ubound(rstable,2)
dim sCrLf,strXML,strXmlClear
sCrLf = chr(13) & chr(10) '回车+换行
sXmlClear = "<?xml version='1.0' encoding='gb2312'?>"
strXML = "<?xml version='1.0' encoding='gb2312'?>"
strXML = strXML & "<selectChoice>"
strXML = strXML & "<selectElement>"
strXML = strXML & "<formName>"&strForm&"</formName>"
strXML = strXML & "<formElement>"&strElem&"</formElement>"
strXML = strXML & "</selectElement>"
if rowc>0 then
strXML = strXML & "<entry>"
strXML = strXML & "<optionText> 选择一个城市 </optionText>"
strXML = strXML & "<optionValue>-1</optionValue>"
strXML = strXML & "</entry>"
for i=0 to rowc
strXML = strXML & "<entry>"
strXML = strXML & "<optionText> "&rstable(1,i)&" </optionText>"
strXML = strXML & "<optionValue> "&rstable(0,i)&" </optionValue>"
strXML = strXML & "</entry>"
next
end if
strXML = strXML & "</selectChoice>"
Response.CharSet="gb2312" '数据集
Response.ContentType="text/xml" '数据流格式定义
'输出:
' Response.write strXmlClear
Response.write strXML
%>
很简单的asp代码吧,ajax代码用现成的框架就好了。
说说中间的排错吧,这个例子用的post方式发送的,返回xml,开始的时候生成的xml老出错,于是我新建一个正常的html表单,显示的提交,查看各个变量的值,结果发现是低级错误,值没传过来。
还有字符串明显错误
最后的关闭总标签 strXML = strXML & "</selectChoice>"
写了成了 strXML = "</selectChoice>"
看《ajax in action》1个月了,想练练,又不知道从何入手,但是今天终于忍不住了。手攘攘的。
拿双组下拉框试试。结果2个小时搞定了,中间卡住了。
书上是第9章,例子是vb.net写的服务端,我要改成asp的。
动手了
我图方便,直接用重构好的组件,全都拷走,用dm8建一个DoubleComboXML.asp,把doubleCombo_refactored.htm改名为doubleCombo_refactored.asp,因为我第一个下拉框也要自动生成。
我要做的效果是:
从数据库调用省份放入第一个select,当省份onchange时,js向服务器查询选中的省份的所属市。
数据库这样设计:
prov表有id,desc ,city表有id,pid,desc
这样编程就不像以前一样集中了,要么全在客户端或服务端一次全部生成。
现在编程就分成了从服务端取出所有省份,查询一个省份的所有城市,还可以再加个查询这个城市的所有县。
无论从编程还是用户体验都是很棒的。
后来发现重构好的组件不工作,就又换成原始的,(不能被挫啊!),
在doubleCombo_refactored.asp(改成asp,为了运行asp代码)加了这个,
<!--#include file="conn.asp"-->
<%
dim sql : sql = "select id, desc from prov "
rs.open sql,conn,1,1
dim rstable : rstable=rs.getrows
rs.close
set rs=nothing
%>
改了这个:
<form id="form">
<select id="region" name="region" >
<option value="-1" selected>选择一个省份</option>
<%
for i=0 to ubound(rstable,2)
response.Write "<option value="""&rstable(0,i)&""">"&rstable(1,i)&"</option>"
next
%>
</select>
<select id="territory" name="territory" />
</form>
DoubleComboXML.asp(提交的url,改成这个,不是aspx的)
<!--#include file="conn.asp"-->
<%
dim strQuery,strForm,strElem,strSql,rstable,rowc
strQuery=cint(request("q"))
strForm=request("f")
strElem=request("e")
strSql="select "&_
"id, desc from city "&_
"where "&_
"pid="&strQuery
rs.open strSql,conn,1,1
rstable=rs.getrows
rs.close
set rs=nothing
rowc=ubound(rstable,2)
dim sCrLf,strXML,strXmlClear
sCrLf = chr(13) & chr(10) '回车+换行
sXmlClear = "<?xml version='1.0' encoding='gb2312'?>"
strXML = "<?xml version='1.0' encoding='gb2312'?>"
strXML = strXML & "<selectChoice>"
strXML = strXML & "<selectElement>"
strXML = strXML & "<formName>"&strForm&"</formName>"
strXML = strXML & "<formElement>"&strElem&"</formElement>"
strXML = strXML & "</selectElement>"
if rowc>0 then
strXML = strXML & "<entry>"
strXML = strXML & "<optionText> 选择一个城市 </optionText>"
strXML = strXML & "<optionValue>-1</optionValue>"
strXML = strXML & "</entry>"
for i=0 to rowc
strXML = strXML & "<entry>"
strXML = strXML & "<optionText> "&rstable(1,i)&" </optionText>"
strXML = strXML & "<optionValue> "&rstable(0,i)&" </optionValue>"
strXML = strXML & "</entry>"
next
end if
strXML = strXML & "</selectChoice>"
Response.CharSet="gb2312" '数据集
Response.ContentType="text/xml" '数据流格式定义
'输出:
' Response.write strXmlClear
Response.write strXML
%>
很简单的asp代码吧,ajax代码用现成的框架就好了。
说说中间的排错吧,这个例子用的post方式发送的,返回xml,开始的时候生成的xml老出错,于是我新建一个正常的html表单,显示的提交,查看各个变量的值,结果发现是低级错误,值没传过来。
还有字符串明显错误
最后的关闭总标签 strXML = strXML & "</selectChoice>"
写了成了 strXML = "</selectChoice>"