在Edge浏览器中选择的下拉菜单更改不起作用。

huangapple go评论90阅读模式
英文:

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 -->

	$(&quot;#btn-search&quot;).click(function () {
            const comingDue = parseInt($(&quot;#comingDueIds option:selected&quot;).val(), 10);
            const duedate = new Date();
            duedate.setDate(duedate.getDate() + comingDue);
            const duesdates =   Date.parse(duedate);
            function dropdownUpdate() {
                $(&#39;.table tbody tr&#39;).each(function () {
                    const comingDates = $(this).find(&#39;td&#39;).eq(2);
                    const dates =comingDates[0].childNodes[2].innerText;
                    const DueDate = Date.parse(dates);
                    $(this).toggle(DueDate &lt;= duesdates );
                });
            }
            $(&#39;#comingDueIds&#39;).on(&#39;change&#39;, dropdownUpdate);
            dropdownUpdate();
       
      
    });

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;select name=&quot;comls&quot; id=&quot;comingDueIds&quot; style=&quot;width: 140px;&quot;&gt;
                                &lt;option value=&quot;&quot; selected=&quot;selected&quot;&gt;Days&lt;/option&gt;
                                &lt;option value=&quot;5&quot;&gt;5 Days&lt;/option&gt;
                                &lt;option value=&quot;10&quot;&gt;10 Days&lt;/option&gt;
                                &lt;option value=&quot;15&quot;&gt;15 Days&lt;/option&gt;
                                &lt;option value=&quot;20&quot;&gt;20 Days&lt;/option&gt;
                                &lt;option value=&quot;25&quot;&gt;25 Days&lt;/option&gt;
                            &lt;/select&gt;

<!-- 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() {
  $(&#39;.table tbody tr&#39;).each(function() {
    const comingDates = $(this).find(&#39;td&#39;).eq(2);
    const dates = comingDates[0].childNodes[2].innerText;
    const DueDate = Date.parse(dates);
    $(this).toggle(DueDate &lt;= duesdates);
  });
}

$(&quot;#btn-search&quot;).click(function() {
  const comingDue = parseInt($(&quot;#comingDueIds option:selected&quot;).val(), 10);
  const duedate = new Date();
  duedate.setDate(duedate.getDate() + comingDue);
  const duesdates = Date.parse(duedate);
});

$(&#39;#comingDueIds&#39;).on(&#39;change&#39;, dropdownUpdate);
dropdownUpdate();

huangapple
  • 本文由 发表于 2023年6月13日 06:03:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定