有没有一种方法可以仅使用Javascript,HTML,CSS在Instant Explorer中执行onChange()(没有Jquery)

时间:2020-06-02 16:17:05

标签: javascript html css

是否有一种方法可以仅使用Javascript,HTML,CSS(无Jquery)在内部资源管理器上执行onChange()。我的代码在onChange()上将输入发送到函数。当我使用chrome时,效果很好。但我只是注意到onChange()在I.E上不起作用,就像在chrome上一样。有没有办法使它像在chrome上一样工作?或者还有其他我可以做的事情,可以在选择一个选项后调用该函数?如果没有,我必须更改脚本代码的所有400行:(

<label>Choose a browser from this list:
        <input id = "input1" list="table1" name="myBrowser" 
        style="width: 500px;"  onchange="castvote('input1','table1')" onclick="test('input1')"/> 
</label>
    <datalist id="table1">
        <option value="Firefox">      
        <option value="more options">

1 个答案:

答案 0 :(得分:1)

下面是一个非常简单的示例,通过香草js添加侦听器。 现在,它已更新为查找诸如castVote的类,并在循环中将侦听器添加到每个元素

(function addCastVoteListeners() {
  const els = Array.prototype.slice.call(document.querySelectorAll('.castVote'));
  els.forEach(addCastVoteListener);
})();
function addCastVoteListener(el) {
  el.addEventListener('change', castVote);
}
function castVote() {
  console.log('cast vote');
}
<label>Choose a browser from this list:
        <input id = "input1" list="table1" name="myBrowser" class="castVote"
        style="width: 500px;"/> 
</label>
<datalist id="table1">
    <option value="Firefox">      
    <option value="more options">
</datalist>