以传统的方式添加事件监听器:
function getComboA(sel) {
var value = sel.options[sel.selectedIndex].value;
}
<select id="comboA" onchange="getComboA(this)">
<option value="">Select combo</option>
<option value="Value1">Text1</option>
<option value="Value2">Text2</option>
<option value="Value3">Text3</option>
</select>
Run Code Online (Sandbox Code Playgroud)
但我想适应addEventListener方式:
productLineSelect.addEventListener('change',getSelection(this),false);
function getSelection(sel){
var value = sel.options[sel.selectedIndex].value;
alert(value);
}
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为我不能将getSelection()中的任何参数作为addEventListener方法中的第二个参数传递?据我所知,我只能使用没有括号的函数名称.
任何的想法?
顺便说一下,请看看我之前关于console.log在safari 6.0开发人员检查器中不起作用的问题,我无法在控制台中编写任何输出,这令人沮丧.