JQuery Onchange事件不工作在生产,但工作在我的本地实例

JQuery Onchange Event Not Working on production but working on my local instance

本文关键字:工作 我的 实例 Onchange 事件 JQuery      更新时间:2023-09-26
$("#time-popup").change(function (event) {
    if (document.getElementById("time-popup") != null) {
        document.getElementById("popup_Billed_").value = document.getElementById("time-popup").value;
    }
});

当点击时间弹出文本框字段时,没有触发事件。甚至没有

提示
alert($("#time-popup"));

请建议

首先你可以像下面这样用jQuery编写(不需要注释代码)

$(document).ready(function(){
    $("#time-popup").change(function (event) {
        /*if ($("#time-popup").length > 0) { */
            $("#popup_Billed_").val($("#time-popup").val());
       /*}*/
       // above if condition not required because you are 
       //running this code for onchange event of time-popup 
       // only so no need to check if it exist or not.
    });
});

检查您的html控制台错误,并确保在页面的视图源中不应该有任何重复的time-popuppopup_Billed Id。

如果时间弹出是动态创建的,那么你应该使用.on()

绑定change event
 $(document).ready(function(){
    $(document).on("change","#time-popup",function (event) {
         $("#popup_Billed_").val($("#time-popup").val());
    });
});

尝试事件委派:

$(document).on('change', "#time-popup", function (event) {
...

From the docs:

事件委托允许我们将单个事件侦听器附加到父元素,该元素将为所有匹配选择器中是否存在这些后代,或者是否已添加到未来。

试试:

$(document).ready(function(){
  $("#time-popup").on('change',function () {
            document.getElementById("popup_Billed_").value = document.getElementById("time-popup").value;
          });
});