2013-02-12 127 views
1

我有這個代碼來顯示文本框中的下拉值。 實際上,當我從文本框中的下拉列表中選擇一個值時,顯示一個值。 如何修改以顯示選項文本insted值?顯示選項文本,而不是值

<select id="Ultra" onchange="run()"> 
    <!--Call run() function--> 
    <option value="0">Select</option> 
    <option value="8">text1</option> 
    <option value="5">text2</option> 
    <option value="4">text3</option> 
</select> 

<br> 
<br> 
TextBox1 
<br> 

<form> 
    <input id="srt" placeholder="get value on option select" type="text"> 
    <br> 
    TextBox2<br> 
    <input id="rtt" onkeyup="up()" placeholder="Write Something !" type="text"> 
    <script> 
    function run() { 
     document.getElementById("srt").value = document.getElementById("Ultra").value; 
    } 

    function up() { 
     //if (document.getElementById("srt").value != "") { 
     var dop = document.getElementById("srt").value; 
     //} 
     pop(dop); 
    } 

    function pop(val) { 
     alert(val); 
    } 
    </script> 
</form> 

回答

2

你需要獲得所選擇的選項,例如文本

<!-- pass reference to the element in the call --> 
<select id="Ultra" onchange="run(this)"> 

腳本:

function run(sel) { 
    var i = sel.selectedIndex; 
    if (i != -1) { 
    document.getElementById("srt").value = sel.options[i].text; 
    } 
} 
+0

RobG非常感謝,作品非常出色。 – user1504222 2013-02-12 03:12:37

0

VAR選擇=的document.getElementById( 「超」);

var selectedText = Select.options [Select.selectedIndex] .text;

+0

請注意,如果未選擇任何選項,'selectedIndex'將爲-1。在這種情況下,'Select.options [Select.selectedIndex]'將返回'undefined',它沒有'text'屬性並且會引發錯誤。 – RobG 2013-02-12 08:34:15

相關問題