[問題] javascript 三層下拉選單請教
我是剛接觸javascript的新人
一直對於javascript指令實在是摸不透
最近使用javascript來寫動態三層下拉選單
目前我已經做出三層式下拉選單成功了
並將選單選擇的VALUE傳送到資料庫儲存
希望下一次要修改這一筆資料時
三層下拉選單可以直接選好前一次的選項
我想要請教的是
1.以我已下程式的寫法為例,
叫出前一次資料時javascript自動選取前一次選項的寫法。
2.以我已下程式的寫法為例,
我原本是想透過比對資料庫與選項value值來選取,
javascript要如何讀取到value呢?
或者是有其他更好的方法可以意見提供呢?
在html有寫到this.options.selectedIndex不知道也是可以用此數值來判斷?
那寫法如何?
完整呈是請參考此網頁
http://big5.webasp.net/javascript/1/282.htm
方法我有略改(選項資料都是寫在資料庫用迴圈去跑)
但是是使用這個網頁的原理
感激好心人的解答
以下是
html=======================================================================
<FORM name="isc">
<!--第一層選單 選項資料都是寫在資料庫用迴圈去跑,在此先以幾筆資料替代-->
<select name="example" size="1"
onChange="redirect(this.options.selectedIndex)">
<option selected>---Select1-------------</option>
<option>Webmaster Sites</option>
<option>News Sites</option>
</select>
<!--第二層選單 選項資料都是寫在資料庫用迴圈去跑,在此先以幾筆資料替代-->
<select name="stage2" size="1"
onChange="redirect1(this.options.selectedIndex)">
<option value=" " selected> </option>
<option value=" " selected>---Select2--------------</option>
</select>
<!--第三層選單 選項資料都是寫在資料庫用迴圈去跑,在此先以幾筆資料替代-->
<select name="stage3" size="1"
onChange="redirect2(this.options.selectedIndex)">
<option value=" " selected> </option>
<option value=" " selected>---Select3----------------</option>
</select>
</form>
以下是
javascript==================================================================
<script language="JavaScript" >
<!--第二層選單寫法 已將選項寫進資料庫去跑迴圈-->
var groups=document.isc.example.options.length
var group=new Array(groups)
for (i=0; i<groups; i++)
group[i]=new Array()
group[1][0]=new Option('請選擇',' ');
.
.
.
略
.
.
.
var temp=document.isc.stage2
function redirect(x){
for (m=temp.options.length-1;m>0;m--)
temp.options[m]=null
for (i=0;i<group[x].length;i++){
temp.options[i]=new Option(group[x][i].text,group[x][i].value)
}
temp.options[0].selected=true
redirect1(0)
<!--第三層選單寫法 已將選項寫進資料庫去跑迴圈-->
var secondGroups=document.isc.stage2.options.length
var secondGroup=new Array(groups)
for (i=0; i<groups; i++) {
secondGroup[i]=new Array(group[i].length)
for (j=0; j<group[i].length; j++) {
secondGroup[i][j]=new Array() }}
secondGroup[0][0][0]=new Option("---Select 3---"," ");
.
.
.
略
.
.
.
var temp1=document.isc.stage3
function redirect1(y){
for (m=temp1.options.length-1;m>0;m--)
temp1.options[m]=null
for
(i=0;i<secondGroup[document.isc.example.options.selectedIndex][y].length;i++){
temp1.options[i]=new
Option(secondGroup[document.isc.example.options.selectedIndex][y][i].text,secondGroup[document.isc.example.options.selectedIndex][y][i].value)
}
temp1.options[0].selected=true
}
function redirect2(z){
window.location=temp1[z].value
}
</script>
請教ptt高手解答
感激
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 222.156.238.216
※ 編輯: vampire1128 來自: 222.156.238.216 (11/02 02:09)
推
11/02 09:27, , 1F
11/02 09:27, 1F
※ 編輯: vampire1128 來自: 222.156.238.216 (11/02 10:18)
→
11/02 10:23, , 2F
11/02 10:23, 2F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章