人気ブログランキング | 話題のタグを見る

:: 簡單生活 :: http://lifestyles.exblog.jp


給訪客的話:要留言請按文章下方的『comment』,『名前』是名字,『パスワード』是password,自由設,用來給留言者刪除自己留的言用的。
by lifestyles
S M T W T F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
カテゴリ
以前の記事
お気に入りブログ
検索
推薦網站
その他のジャンル
ファン
記事ランキング
ブログジャンル
画像一覧

兩個multiple的select欄位的資料搬移

昨、今天一直在處理的問題,搞了很久終於完成了,在JavaScript裡面調整試卷的順序之後,再送到後端輸入至資料庫中。
用兩個multiple的select欄位做資料搬移,因為要送給PHP,所以SELECT欄位採用pageSerial[]作名稱。



※試題搬移到試卷裡,將itemSerial欄位中,所有的選項瀏覽過一次,有被選擇過的(selected),就把它丟到pageSerial欄位的最底下。
function item2page(){
for(ctr=0;ctr<document.getElementById("itemSerial").length;ctr++){
if (document.getElementById("itemSerial").options[ctr].selected == true){
document.getElementById("pageSerial").options[document.getElementById("pageSerial").length]= new Option(document.getElementById("itemSerial").options[ctr].text,document.getElementById("itemSerial").options[ctr].value);
}
}
}
※將試題從試卷中移除。
function page2item(){
for(ctr=0;ctr<document.getElementById("pageSerial").length;ctr++){
if(document.getElementById("pageSerial").options[ctr].selected == true){
document.getElementById("pageSerial").remove(ctr);
※因為已經移除了,所以ctr必須減一。
ctr--;
}
}
}
※將pageSerial中所有欄位設為selected才送出,不然一個都抓不到。
function makeVolume(){
for(ctr=0;ctr<document.getElementById("pageSerial").length;ctr++){
document.getElementById("pageSerial").options[ctr].selected = true;
}
document.volumeForm.submit();
}
※將試題的順序往上提。
function moveUp(id){
※紀錄被搬移的試題idx用。
tmp_array = new Array();
tmp_idx = 0;
※避免第一個選項往上搬移會出錯,所以從1開始。
for(ctr=1;ctr<document.getElementById(id).length;ctr++){
if(document.getElementById(id).options[ctr].selected == true){
※這裡做的其實是swap的工作。
tmp_text = document.getElementById(id).options[ctr-1].text;
tmp_value = document.getElementById(id).options[ctr-1].value;
document.getElementById(id).options[ctr-1] = new Option(document.getElementById(id).options[ctr].text, document.getElementById(id).options[ctr].value);
document.getElementById(id).options[ctr] = new Option(tmp_text, tmp_value);
tmp_array[tmp_idx++] = ctr-1;
}
}
※最後一個動作,把剛剛搬移過的試題selected,這樣可以立即搬移到下一個位置。
for(ctr=0;ctr<tmp_array.length;ctr++){
document.getElementById(id).options[tmp_array[ctr]].selected = true;
}
}
function moveDown(id){
tmp_array = new Array();
tmp_idx = 0;
for(ctr=document.getElementById(id).length-2;ctr>=0;ctr--){
if(document.getElementById(id).options[ctr].selected == true){
tmp_text = document.getElementById(id).options[ctr+1].text;
tmp_value = document.getElementById(id).options[ctr+1].value;
document.getElementById(id).options[ctr+1] = new Option(document.getElementById(id).options[ctr].text, document.getElementById(id).options[ctr].value);
document.getElementById(id).options[ctr] = new Option(tmp_text, tmp_value);
tmp_array[tmp_idx++] = ctr+1;
}
}
for(ctr=0;ctr<tmp_array.length;ctr++){
document.getElementById(id).options[tmp_array[ctr]].selected = true;
}
}
※因為name使用pageSerial[]這樣的格式,所以必須加上id的設定,才可以順利處理,這個function就是用來取得id指到的欄位。
function GetElementById(id){
if (document.getElementById) {
return (document.getElementById(id));
} else if (document.all) {
return (document.all[id]);
} else {
if ((navigator.appname.indexOf("Netscape") != -1) && parseInt(navigator.appversion == 4)) {
return (document.layers[id]);
}
}
}

by lifestyles | 2005-01-25 19:35 | 技術筆記
<< 塊魂2-大家都喜愛塊魂 繳費地點有夠遠 >>