ASP中SELECT下拉選單同時獲取VALUE和TEXT值的實現程式碼
阿新 • • 發佈:2020-06-18
在1個註冊頁面1.asp,先選擇鎮,如下程式碼:
<form action="reguser2.asp" method="post" name="form1" onSubmit="return checksumit();"> <table width="100%" border="1" cellspacing="0" cellpadding="0"> <tr> <td>所在鎮: <% sql="select * from zhen" set rs=server.createobject("adodb.recordset") rs.open sql,conn,3,2 %> <select name="zhen" id="zhen"> <option value=-1>選擇鎮</option> <% if rs.eof and rs.bof then response.Write("<option value=-1>沒有鎮</option>") else do while not rs.eof %> <option value="<%=rs("z_id")%>"><%=rs("z_name")%></option> //這裡value 和 text 的值 在資料庫中獲得,但變數"zhen" 只能獲得value的值...如何獲得選中的 text值呢? <% rs.movenext loop end if%> </select></td> </tr> <tr align="center"> <td><input type="submit" name="Submit" value="下一步" onclick="Javascript:callvalue()"></td> //第三步,傳遞到下一頁按鈕這裡增加 onclick取得值 </tr> </table> <input type="hidden" name ="sendvalue" /> //第一步,這裡增加一個hidden的input控制元件 <script language="JavaScript"> //第二步,這裡寫一個獲得text值的函式 function callvalue() { sendvalue = document.form1.zhen.item(document.form1.zhen.selectedIndex).text; //alert(sendvalue); document.form1.sendvalue.value = sendvalue; form1.submit(); } </script>
最後,在另外一個頁面 2.asp 裡面直接取選定的text值 request.Form("sendvalue")
這樣,可以不頻繁讀取資料庫取得值