英文:
selected dropdown change not working in Edge browser
问题
以下是您要翻译的内容:
"I have a selected option select items and using jquery on change to update a search. The functionality works in IE11 but not edge. In Edge, after the first item is selected the change event doesn't work after that. If I select from the last item, it will change, but as soon as the first item on the list is selected, the change event doesn't work anymore. Below is a portion of my code.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$("#btn-search").click(function () {
const comingDue = parseInt($("#comingDueIds option:selected").val(), 10);
const duedate = new Date();
duedate.setDate(duedate.getDate() + comingDue);
const duesdates = Date.parse(duedate);
function dropdownUpdate() {
$('.table tbody tr').each(function () {
const comingDates = $(this).find('td').eq(2);
const dates = comingDates[0].childNodes[2].innerText;
const DueDate = Date.parse(dates);
$(this).toggle(DueDate <= duesdates);
});
}
$('#comingDueIds').on('change', dropdownUpdate);
dropdownUpdate();
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="comls" id="comingDueIds" style="width: 140px;">
<option value="" selected="selected">Days</option>
<option value="5">5 Days</option>
<option value="10">10 Days</option>
<option value="15">15 Days</option>
<option value="20">20 Days</option>
<option value="25">25 Days</option>
</select>
<!-- end snippet -->
英文:
I have a selected option select items and using jquery on change to update a search. The functionality works in IE11 but not edge. In Edge, after the first item is selected the change event doesn't work after that. If I select from the last item, it will change, but as soon as the first item on the list is selected, the change event doesn't work anymore. Below is a portion of my code.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$("#btn-search").click(function () {
const comingDue = parseInt($("#comingDueIds option:selected").val(), 10);
const duedate = new Date();
duedate.setDate(duedate.getDate() + comingDue);
const duesdates = Date.parse(duedate);
function dropdownUpdate() {
$('.table tbody tr').each(function () {
const comingDates = $(this).find('td').eq(2);
const dates =comingDates[0].childNodes[2].innerText;
const DueDate = Date.parse(dates);
$(this).toggle(DueDate <= duesdates );
});
}
$('#comingDueIds').on('change', dropdownUpdate);
dropdownUpdate();
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="comls" id="comingDueIds" style="width: 140px;">
<option value="" selected="selected">Days</option>
<option value="5">5 Days</option>
<option value="10">10 Days</option>
<option value="15">15 Days</option>
<option value="20">20 Days</option>
<option value="25">25 Days</option>
</select>
<!-- end snippet -->
答案1
得分: 0
我会将 dropdownUpdate 和调用它的事件处理程序放在点击处理程序之外。如果将其放在点击处理程序内部,可能会出现奇怪的问题。
如果需要确保必须点击 btn-search,您可以始终添加某种变量的条件语句。但您不需要将它放在里面。
function dropdownUpdate() {
$('.table tbody tr').each(function() {
const comingDates = $(this).find('td').eq(2);
const dates = comingDates[0].childNodes[2].innerText;
const DueDate = Date.parse(dates);
$(this).toggle(DueDate <= duesdates);
});
}
$("#btn-search").click(function() {
const comingDue = parseInt($("#comingDueIds option:selected").val(), 10);
const duedate = new Date();
duedate.setDate(duedate.getDate() + comingDue);
const duesdates = Date.parse(duedate);
});
$('#comingDueIds').on('change', dropdownUpdate);
dropdownUpdate();
英文:
I would put dropdownUpdate and the event handler that calls it outside of the click handler. You are going to have weird problems when putting it inside of the click handler.
If you require the btn-search to be clicked, you can always add some sort of if statement for a variable. But you don't need it in there.
function dropdownUpdate() {
$('.table tbody tr').each(function() {
const comingDates = $(this).find('td').eq(2);
const dates = comingDates[0].childNodes[2].innerText;
const DueDate = Date.parse(dates);
$(this).toggle(DueDate <= duesdates);
});
}
$("#btn-search").click(function() {
const comingDue = parseInt($("#comingDueIds option:selected").val(), 10);
const duedate = new Date();
duedate.setDate(duedate.getDate() + comingDue);
const duesdates = Date.parse(duedate);
});
$('#comingDueIds').on('change', dropdownUpdate);
dropdownUpdate();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论