[英]OnSelect equivalent for <option>?
我有一個<select>
標簽,我希望它在選擇一個選項時更改隱藏輸入的值。 我想是用這個:
<input type=hidden value=Ineedtochangethis name=asdf>
<select><option onselect="stuff()">Option 1 is selected</option>...</select>
然后這個
function stuff() {
document.getElementsByName("asdf").value=opt1slct;
}
當然,它不起作用。 我能做些什么來獲得類似的效果?
please check following code:
<select onChange="jsFunction()" id="selectOpt">
<option value="1" >1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
function jsFunction(){
var myselect = document.getElementById("selectOpt");
if(myselect.options[myselect.selectedIndex].value == 1){
alert('hide');
}else{
alert('show');
}
}
通過使用select
元素的selectedIndex
屬性(可能還有一個options
)。
<input id="yourHiddenField" type="hidden"/>
<select onchange="stuff(this);">
<option>Option 1 is selected</option>
</select>
和JavaScript:
function stuff(yourSelectElement){
document.getElementById('yourHiddenField').value = yourSelectElement.options[yourSelectElement.selectedIndex].innerHTML;
}
如果您只想在隱藏字段中存儲一個數字,請使用option
標記上的value
屬性,如下所示:
<option value="1">Option 1 is selected</option>
然后JS改變如下:
document.getElementById('yourHiddenField').value = yourSelectElement.options[yourSelectElement.selectedIndex].value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.