提问人:Sanju Basnet 提问时间:5/8/2023 更新时间:5/8/2023 访问量:23
Money-rails、gem rails 和 js dynamic select of dropdown
Money-rails gem rails and js dynamic select of dropdown
问:
我有种子国家,有名称、number_code和 iso。对于货币,我使用了 money-rails gem。当我选择国家/地区时,我希望用户自动选择其相关货币。当我选择美国自动选择美元时,如果用户想更改货币,他们也可以更改它。
我已经用ajax完成了这个。
我在控制器中有方法get_currency我得到了响应。{"currency":{"name":"Aruban Florin","symbol":"ƒ","iso_code":"AWG"}}
// country, currency select dropdown start
var countryDropdown = $('#country-select');
var currencyDropdown = $('#currency-select');
// Handle country dropdown change and set initial state of currency dropdown
function updateCurrencyDropdown(countryId) {
$.ajax({
url: '/company_countries/get_currency',
type: 'GET',
data: { country_id: countryId },
success: function(response) {
var currency = response.currency;
var currencyOption;
var selectedCurrency = currencyDropdown.val();
if (currency && currency.name) {
console.log(currency)
currencyOption = `<option value="${currency.iso_code}">${currency.name} (${currency.symbol})</option>`;
console.log(currencyOption)
currencyDropdown.find(`option[value="${currency.iso_code}"]`).remove(); // remove currency option from the dropdown
currencyDropdown.prepend(currencyOption); // Add new currency option at the top
if (selectedCurrency !== currency.iso_code) {
currencyDropdown.val(currency.iso_code);
console.log("currencyDropdown", currencyDropdown)
}
} else {
currencyOption = '<option value="USD">US Dollar (USD)</option>';
currencyDropdown.empty().append(currencyOption); // Clear previous options and add default option
}
}
});
}
// Set initial state of currency dropdown based on initial value of country dropdown
var initialCountryId = countryDropdown.val();
updateCurrencyDropdown(initialCountryId);
// Handle country dropdown change
countryDropdown.on('change', function() {
var countryId = $(this).val();
updateCurrencyDropdown(countryId);
});
// country, currency select dropdown end
我在这里只有 1 或 2 个选择。如果我选择奥地利,则获得欧元和美元。其他货币列表作为选项呢?
答: 暂无答案
评论