简体   繁体   English

jQuery/Javascript - 当浏览器控制台中的 val 发生变化时,检测 select 选项的变化

[英]jQuery/Javascript - detect changes in select option when val is change in browsers console

I have a code that triggers when the select option is changed, however when the value is changed in browsers console it will not detect the change event.我有一个代码在 select 选项更改时触发,但是当浏览器控制台中的值更改时,它不会检测到更改事件。

$("#additional_myfield3").on('change', function(){
// recalculation of shipping cost
});

UPDATE This is in WooCommerce, changes in select option will update the shipping cost.更新这是在 WooCommerce 中,更改 select 选项将更新运费。 By default shipping is $0, but when changed it will recalculate and will range from $5-$10.默认情况下运费为 0 美元,但更改后会重新计算,范围为 5 美元至 10 美元。

  • Changing value in console在控制台中更改值
jQuery('select#additional_myfield3').val('Location 1');

The problem is when option is changed via console, it will not update the shipping cost.问题是通过控制台更改选项时,它不会更新运费。 In backend the TEAM is changed to "Location 1" but the shipping cost is still 0, when it should be $5.在后端,团队更改为“位置 1”,但运费仍为 0,此时应为 5 美元。

Make sure to initialize this after the page has loaded, as in确保在页面加载后初始化它,如

window.onload = function() {
  $("#additional_myfield3").on('change', function(){
     alert('changed');
  });
}

 $("#additional_myfield3").on('change', function() { alert('changed'); }); //on console should trigger change $("#additional_myfield3").val("2").trigger("change");
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- on input should trigger change --> <select id="additional_myfield3"> <option value="1">1 <option value="2">2 </select>

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

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