[英]selecting an option in a select statement from onclick event
我想基於onclick調用的javascript函數在選擇菜單中選擇一個選項。
可見,用戶可以選擇鮑勃得到多少根睫毛,但是如果用戶單擊該按鈕,它將從下拉列表中選擇“ 1”。
基本上,我想基於Java腳本事件設置下拉菜單的默認選項的值。 這是我舉個例子的jsfiddle。 我已經嘗試了w3schools.com信息庫中的幾乎所有事件,而我幾乎已經放棄了。
How Many lashes would Bob like?
<select id="lashes" name="lashes">
<option value="five">5</option>
<option value="four">4</option>
<option value="three">3</option>
<option value="two">2</option>
<option value="one">1</option>
<option value="zero">0</option>
</select>
<input type="button" value="Bob actually wants only 1 lashing" onclick="doit()">
function doit() {
document.getElementByID('lashes') = 'one';
}
http://jsfiddle.net/pietbarber/H8Ctx/
另外,如果我可以擺脫它,我也不想使用任何JavaScript庫,因此不需要jsquery等等。
TL; DR-我想單擊該按鈕,並在單擊該按鈕時從下拉菜單中選擇“ 1”。
最簡單的方法是使用select
元素的selectedIndex
屬性。
function doit() {
//notice I changed .getElementByID to .getElementById
document.getElementById('lashes').selectedIndex = 4;
}
它應該是
function doit() {
document.getElementById('lashes').value = 'one';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.