2013-02-12 126 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

相关问题