語法錯誤 - 選項上沒有名稱。也請在報價上保持一致
請向我們展示select()函數,它是失敗的。
工作例如:http://jsfiddle.net/mplungjan/fXacE/
注意,腳本,我建議需要留名ID的工作,但如果你使用的document.getElementById你必須有一個ID,因爲只有IE瀏覽器將超載的getElementById也得到命名元素
<script>
var currenCurrency="";
function setCurrency(sel) {
currentCurrency=sel.options[sel.selectedIndex].value
alert(currentCurrency);
}
</script>
<form name="form1">
Currency:<br><select name="currency" id="currency" onchange="setCurrency(this)">
<option value="">Please select</option>
<option value="usd">USD</option>
<option value="euro">Euro</option>
<option value="won">Won</option>
</select>
</form>
如果你不想內嵌事件處理程序,然後以純JS,你可以使用
var currentCurrency="";
window.onload=function() {
document.getElementById("currency").onchange=function() {
currentCurrency=this.options[this.selectedIndex].value
}
document.getElementById("currency").onchange(); // set the current value
}
如果MUS T選用jQuery的,使用
$("#currency option:selected").text()
或
$("#currency option:selected").val()
對select元素使用* change *事件需要仔細考慮和設計。如果正在使用鍵盤導航,則每次按下光標鍵以選擇新選項時,某些瀏覽器(例如IE)將觸發更改事件。特別是,從不使用onchange進行導航。 – RobG 2011-06-10 05:24:25
自1995年以來,我從來沒有在選擇onchange任何問題。我沒有看到你會遇到什麼問題。 – mplungjan 2011-06-10 05:29:39
@mplungian:1995年有沒有一個onchange處理程序? Javascript(Livescript)在1995年12月看到了它的第一個燈光:〜) – KooiInc 2011-06-10 05:54:21