2013-12-23 32 views
0

我有這個JavaScript的下拉菜單,但我不知道如何從中選擇的值。如何選擇一個JavaScript下拉菜單,並把它放在asp.net中的標籤

<select name="slist" > 
    <script language="javascript"> 
     var states = new Array("Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antarctica"); 
     for (var hi = 0; hi < states.length; hi++) 
      document.write("<option value=\"" + states[hi] + "\">" + states[hi] + "</option>"); 
      document.getSelection(states); 

    </script> 
</select> 

我已經使用這個命令嘗試:
document.getElementById("Label1").InnerHTML = states;
,但沒有奏效。如何創建一個函數,讓我在label.text中選定的值? p.s.下拉菜單取自www.hscripts.com

我忘了提及我在更新面板中有這個菜單,因爲我需要一個無閃爍的回發。當我點擊我的提交按鈕時,菜單會丟失它的內容,爲什麼?

回答

0

試試這個

爲了得到選擇的值追加選擇框

var e = document.getElementById("slist"); 
var strUser = e.options[e.selectedIndex].value; //Getting Value 
document.getElementById("Label1").InnerHTML = strUser //Appending value to your label 
0

後,您可以嘗試這樣的:

<select id="slist" name="slist" onchange="getSelectedState()"> 
.................... 
</select> 

你的JavaScript函數:

function getSelectedState(){ 
    var elem = document.getElementById("slist"); //Get Element here 
    var state = elem.options[elem.selectedIndex].text; //Get value from dropdown or you can get text also 
    document.getElementById("selectedState").innerText = state; // initialize label here 
} 

JS Fiddle Demo

+0

你的答案並不支持他.. !! –

+0

@Arnrnath你怎麼能這麼說? –

+0

你見過他的編碼嗎?他說javascript下拉.. !! –

1

這個怎麼樣:

<select name="slist" id="slist"> 
    <script language="javascript"> 
     var states = new Array("Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antarctica"); 
     for (var hi = 0; hi < states.length; hi++) 
      document.write("<option value=\"" + states[hi] + "\">" + states[hi] + "</option>"); 
      document.getSelection(states); 

     var sel = document.getElementById("slist"); 
     sel.addEventListener('change', function() { 
      document.getElementById("Label1").innerHTML = sel.value; 
     }); 
    </script> 
</select> 
<div id="Label1"></div> 
相關問題