简体   繁体   English

Javascript:: addEventListener on change 单选按钮无法使用 requirejs

[英]Javascript:: addEventListener on change radio button not working using requirejs

Requirejs addEventListener on change radio button not working in magento Requirejs addEventListener on change 单选按钮在 magento 中不起作用

Here everything works fine just one issue is there, it doesn't the value and id of input radio fields, So I think addEventListener DOMContentLoaded is not working here.在这里一切正常,只有一个问题,它不是输入单选字段的值和 id,所以我认为 addEventListener DOMContentLoaded 在这里不起作用。

Help is really appreciated TIA非常感谢您的帮助 TIA

This is js file code这是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;
});

Just add click function for change of radio button只需添加单击 function 即可更改单选按钮

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));
          });
    }
});

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

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