2010-04-23 59 views
1

所有值我有一個窗體上的選擇框 - 我已經在一個列表框中通過把後在選擇框/列表框

<select id="Select1" name="D1" size="5" style="width: 220px"> 

我填充值這個選擇/列表框打開..

當我發佈表單時,如何獲得選擇框中的所有值..這是可能的,還是我只能得到一個已被選中的值。

麻煩的是我想在選擇的所有值(我沒有任何選擇這樣)

任何想法?

回答

1

之前提交表單,您可以使用JavaScript來拉動項目進行選擇,並把它們變成一個隱藏的文本字段(如分隔字符串)

例如,你可以使用

var select1 = document.getElementById('select1'); 
var values = new Array(); 

for(var i=0; i < select1.options.length; i++){ 
    values.push(select1.options[i].value); 
} 

var allValues = values.join(";"); 
alert(allValues); 

希望有所幫助。

+0

好的。我認爲這是要走的路。但我試圖在選擇中獲取每個值,但是在獲取值時遇到問題。我有什麼是: function doSelects(){ var select1 = document.getElementById(「select1」); (s1 = 1; s1 <= select1.length; s1 ++) { alert(select1.options [s1] .value); } } ......怎麼輸出每個值? – thegunner 2010-04-23 12:42:31

+0

它是select1.options [s1] .value這顯然是不正確的,但不知道正確的語法 – thegunner 2010-04-23 12:43:27

+0

我已經更新了我的答案與一個適用於我的例子。您將希望從零開始運行for循環,並且以小於options.length的一個結尾,因爲選項是基於零的數組。 – 2010-04-23 14:46:53

1

你如何將值添加到列表框?它們是靜態還是從數據庫中提取?

如果您從數據庫中提取數據,我會創建一個函數用於獲取數據並綁定到列表框。

然後當你想在帖子後面獲得這些值時使用相同的功能。您可能必須使用一些隱藏字段來傳遞您用來首先獲取列表框值的任何參數。

例如:

function get_models_for_make(int make_id) 
    mydata_rs = SELECT name, id FROM models WHERE make_id = make_id 
    return mydata_rs 
end 

,所以你可以使用這個數據的對象綁定到你的列表框,還可以使用它以後得到的數值,你沒有綁定到你的列表框。

-1
for (int i = source.Items.Count - 1; i >= 0; i--) 
     { 
      ListItem item = source.Items[i]; 

      if (moveAllItems) 
       item.Selected = true; 

      if (item.Selected) 
      { 
       // if the target already contains items, loop through 
       // them to place this new item in correct sorted order 
       if (target.Items.Count > 0) 
       { 
        for (int j = 0; j < target.Items.Count; j++) 
        { 
         if (target.Items[j].Text.CompareTo(item.Text) > 0) 
         { 
          target.Items.Insert(j, item); 
          item.Selected = false; 
          break; 
         } 
        } 
       } 

       // if item is still selected, it must be appended 
       if (item.Selected) 
       { 
        target.Items.Add(item); 
        item.Selected = false; 
       } 

       // remove the item from the source list 
       source.Items.Remove(item); 
      } 
     } 
+0

此評論對發佈無效。有些上下文會很好。這是服務器端還是客戶端?你忘記了宣言嗎?什麼是moveAllItems? – Barett 2012-12-06 23:28:04