简体   繁体   English

更改 function 在 Edge 或 IE 中未触发

[英]Change function not firing in Edge or IE

I'm trying to figure out how to get the change function to fire on IE and Edge.我试图弄清楚如何让更改 function 在 IE 和 Edge 上触发。 I'm trying to have two select fields feed into a hidden input, overriding any previous selected values.我试图将两个 select 字段输入隐藏输入,覆盖任何先前选择的值。 For example, I select option 1 from the first select, 1 will be the value of the hidden input.例如,我从第一个 select 中选择 select 选项 1,1 将是隐藏输入的值。 If I select option 3 from the second select, 3 will be the new value of the hidden input.如果我从第二个 select 中选择 select 选项 3,则 3 将是隐藏输入的新值。

<select id="select1">
<option value="1">1</option>
<option value="2">2</option>
</select>

<select id="select2">
<option value="3">3</option>
<option value="4">4</option>
</select>

<input type="hidden" id="hiddenInput">

<script>
$(document).ready(function() {
$('#select1').on("change", function() {
   $('#hiddenInput').val($(this).val());
});
$('#select2').on("change", function() {
   $('#hiddenInput').val($(this).val());
});
});
</script>

Although your code should work there's no need to call the 'on' method twice.尽管您的代码应该可以工作,但无需两次调用“on”方法。 You can call it once by using the element as a selector or assign a class to it.您可以通过将元素用作选择器来调用它一次,或为其分配 class。

Ex:前任:

$('select').on('change', function() {
    $('#hiddenInput').val($(this).val());
});

or或者

$('.selectClass').on('change', function() {
    $('#hiddenInput').val($(this).val());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM