单击“选项”时如何调用函数(HTML 选择 > 选项) 当该元素是在 javascript 中创建而在 html 中不存在时? 这里我没有使用 jquery 或 ajax,只是 javascript
在我的 html 中我只有:
<fieldset>
<legend>List...</legend>
<select id="select1" size="9" style="width:150px">
<option>Select a person</option>
</select>
</fieldset>
- 更多选项(人员)在另一个功能中创建
最佳答案
使用onchange事件
不使用 jquery
document.body.onchange = function(e) {
if (e.target.tagName=='SELECT'){
SelectedOption(e.target);
}
}
function SelectedOption(option){
alert(option.value);
alert(option.id);
}
选择控件的另一个例子
<select onchange="javascript:SelectedOption(event.target)" > ...
关于javascript - html/javascript : how to onclick on non-existent element in html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38938675/