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>
4

2 回答 2

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;
  }
}
于 2013-02-12T03:07:46.097 回答
0

var Select = document.getElementById("Ultra");

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

于 2013-02-12T03:07:58.893 回答