[英]How do I move the onclik text to move to the text area?
當我點擊蘋果時
蘋果的文本區,不是數字。
function getIndex() { document.getElementById("demo").innerHTML = document.getElementById("mySelect").selectedIndex; }
<select id="mySelect"> <option>Apple</option> <option>Orange</option> <option>Pineapple</option> <option>Banana</option> </select> <input type="button" onclick="getIndex()" value="Display the index of the selected option"> <p id="demo"></p>
我編譯 select 選項中的值,我希望文本出現而不是數字
使用option[index].text
獲取選中的索引並獲取對應選項的值
<head> <script> function getIndex() { var index = document.getElementById("mySelect").selectedIndex; document.getElementById("demo").innerHTML = document.getElementById("mySelect").options[index].text; } </script> </head> <body> <select id="mySelect"> <option>Apple</option> <option>Orange</option> <option>Pineapple</option> <option>Banana</option> </select> <input type="button" onclick="getIndex()" value="Display the index of the selected option"> <p id="demo"></p>
替換這一行document.getElementById("mySelect").selectedIndex;
與document.getElementById("mySelect").value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.