簡體   English   中英

Javascript:: addEventListener on change 單選按鈕無法使用 requirejs

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

Requirejs addEventListener on change 單選按鈕在 magento 中不起作用

在這里一切正常,只有一個問題,它不是輸入單選字段的值和 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;
});

只需添加單擊 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