【问题标题】:Javascript:: addEventListener on change radio button not working using requirejsJavascript:: 更改单选按钮上的 addEventListener 无法使用 requirejs
【发布时间】:2021-06-10 05:35:40
【问题描述】:

Requirejs addEventListener on change 单选按钮在 magento 中不起作用

这里一切正常,只有一个问题,它不是输入单选字段的 value 和 id,所以我认为 addEventListener DOMContentLoaded 在这里不起作用。

非常感谢 TIA 的帮助

这是js文件代码

define([
    "jquery",
    "domReady!"
], function($){
    "use strict";
        const myscript=function(){
            const profit = document.querySelector('input[name="rdmonths"]:checked').value;
            const id = document.querySelector('input[name="rdmonths"]:checked').id;
            const principal = document.querySelector('input[name="investment_calc"]').value;
            const time = id;
            const rate = profit/100;
            const n = 1;
        
            const compoundInterest = (p, t, r, n) => {
               const amount = p * (Math.pow((1 + (r / n)), t));
               return amount;
            };
        
            document.getElementById("total-amount").innerHTML = (compoundInterest(principal, time, rate, n).toFixed(2));   
            console.log(compoundInterest(principal, time, rate, n).toFixed(2));
        }
        
        document.addEventListener('DOMContentLoaded',()=>{
            document.querySelectorAll('input[type="radio"][name="rdmonths"]').forEach(input=>{
                input.addEventListener('click', myscript )
            })
        })

    return myscript;
});

【问题讨论】:

    标签: javascript dom onclick requirejs addeventlistener


    【解决方案1】:

    只需添加单击功能即可更改单选按钮

    define([
        "jquery",
        "domReady!"
    ], function($){
        "use strict";
        return function myscript()
        {
            const profit = document.querySelector('input[name="rdmonths"]:checked').value;
                const id = document.querySelector('input[name="rdmonths"]:checked').id;
                const principal = document.querySelector('input[name="investment_calc"]').value;
                const time = id;
                const rate = profit/100;
                const n = 1;
            
                const compoundInterest = (p, t, r, n) => {
                   const amount = p * (Math.pow((1 + (r / n)), t));
                   return amount;
                };
                document.getElementById("total-amount").innerHTML = (compoundInterest(principal, time, rate, n).toFixed(2));               
            jQuery(".months").click(function(){
                const profit = document.querySelector('input[name="rdmonths"]:checked').value;
                const id = document.querySelector('input[name="rdmonths"]:checked').id;
                const principal = document.querySelector('input[name="investment_calc"]').value;
                const time = id;
                const rate = profit/100;
                const n = 1;
            
                const compoundInterest = (p, t, r, n) => {
                   const amount = p * (Math.pow((1 + (r / n)), t));
                   return amount;
                };
                document.getElementById("total-amount").innerHTML = (compoundInterest(principal, time, rate, n).toFixed(2));
              });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 2021-12-14
      • 2015-04-03
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多