javascript - 如何使用 Javascript 设置默认选项?

标签 javascript

我正在尝试为游戏市场构建一个货币转换器,并且我目前拥有它,以便每种货币/付款方式的相应图标出现在输入栏中 - 它有一个付款选项的默认选项,但在用户单击某个国家/地区之前,跨度为空白。如何设置默认值以使图标 100% 出现?

我设法使其与付款方式图标一起使用,但我缺少国家/地区图标的某些内容

https://codepen.io/anon/pen/byXrWN

    const data = [
    {
        currency: 'paypal',
        we_buy: 0.50,
        we_sell: 0.68,
        img_path: 'img/paypal.svg',
        icon: 'fab fa-paypal'
    },
    {
        currency: 'debit',
        we_buy: 0.67,
        we_sell: 0.82,
        img_path: 'img/debit-card.svg',
        icon: 'far fa-credit-card'
    },
    {
        currency: 'btc',
        we_buy: 0.58,
        we_sell: 0.77,
        img_path: 'img/bitcoin.svg',
        icon: 'fab fa-btc'
    },
    {
        currency: 'ethereum',
        we_buy: 0.59,
        we_sell: 0.76,
        img_path: 'img/ethereum.svg',
        icon: 'fab fa-ethereum'
    }
];

const country = [
    {
        country_id: 'USA',
        country_currency: 'USD',
        img_path: 'img/united-states.svg',
        icon: 'fas fa-dollar-sign',
        rate: 1.0
    },
    {
        country_id: 'EUR',
        country_currency: 'EUR',
        img_path: 'img/european-union.svg',
        icon: 'fas fa-euro-sign',
        rate: 0.88
    },
    {
        country_id: 'UK',
        country_currency: 'GBP',
        img_path: 'img/united-kingdom.svg',
        icon: 'fas fa-pound-sign',
        rate: 0.78
    },  
    {
        country_id: 'CAN',
        country_currency: 'CAD',
        img_path: 'img/canada.svg',
        icon: 'fas fa-dollar-sign',
        rate: 1.33
    }
];

const countryContainer = document.getElementById("countries");
let selectedCountry = null;
var selectCountry = function (index) {
    const cdata = country[index];
    selectedCountry = country[index];
    document.getElementById("country-selected").innerHTML = `Country selected: ${cdata.country_currency}`;
    document.getElementById("country_icon").className = cdata.icon;
};

// Image container

const imagesContainer = document.getElementById("currencies");
let selectedCurrency = null;
var selectCurrency = function (index) {
    const element = data[index];
    selectedCurrency = data[index];
    document.getElementById("currency-selected").innerHTML = `Currency selected: ${element.currency}`;
    document.getElementById("data_icon").className = element.icon; 
};

// made function originally in amount.onkeyup have a greater scope
const calculate = () => {
    const output_we_sell = document.getElementById("output_we_sell");
    if (amount.value === '') {
        output_we_sell.innerHTML = 0;
        return;
    }
    if (!isNaN(amount.value)) {
        output_we_sell.innerHTML = `${(+amount.value * selectedCurrency.we_sell).toFixed(2)}`;
    }
};
// ...
var selectCurrency = function (index) {
    const element = data[index];
    selectedCurrency = data[index];
    document.getElementById("currency-selected").innerHTML = `Currency selected: ${element.currency}`;
    document.getElementById("data_icon").className = element.icon;
    calculate(); // Added calculate here
};

var selectCountry = function (index) {
    const cdata = country[index];
    selectedCountry = country[index];
    document.getElementById("country-selected").innerHTML = `Currency selected: ${cdata.country}`;
    document.getElementById("country_icon").className = cdata.icon;
    calculate();
};

// ...
(() => {
    for (let i = 0; i < data.length; i++) {
        imagesContainer.innerHTML += `<div class="currency" onclick=selectCurrency(${i})><img id=${i} src=${data[i].img_path}></div>`;
    }
    selectCurrency(0);
    const amount = document.getElementById("amount");
    amount.onkeyup = calculate; // Changed this to use the calculate function{
    for (let i = 0; i < country.length; i++) {
        countryContainer.innerHTML += `<div class="country" onclick=selectCountry(${i})><img id=${i} src=${country[i].img_path}></div>`; 
    }
}

)();

顶行是国家/地区选择,底行是付款方式(在 html/js 中命名不佳 - 货币 = 付款方式)

付款方式的图标应在页面加载时出现,而不是在用户点击其原籍国后出现

最佳答案

我查看了您的代码,发现您没有调用函数来设置默认国家/地区。只需将 selectCountry(0); 添加到第 117 行即可。

关于javascript - 如何使用 Javascript 设置默认选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56506101/

相关文章:

javascript - 无法读取未定义的属性 'getTime'

javascript - react native -调试[对象对象]handleException-Observable

Javascript 数组复制,concat vs slice,哪个更好?

javascript - 如何从多个不同长度的数组创建一个设定长度的新数组,根据重要性从每个较小的数组中取出的项目数

javascript - D3 条形图上文本标签的定位

javascript - 如何在Jquery中获取响应 session cookie

Javascript序列化

javascript - 使用选择器获取最近的父元素(不包括当前元素)

javascript - 附加到文本区域,后跟新行

javascript - 在字符串末尾增加一个数字