< select>选项取决于另一个选项的选项< select>菜单
最近,我开始了JavaScript课程,因此我仍在弄清楚如何做最好的事情。我的目标是拥有2个选择菜单,包括相同的货币及其价值,并在彼此之间进行转换。
例如,如果菜单中的欧元从菜单B组合在一起,则为1 *1。美元将为1 * 1.06,依此类推,但这仅允许我从欧元转换为其他东西。
我试图通过循环弄清楚这是否可以。那会以某种方式工作吗?
编辑: 在Mstephen19给出的HTML中,我表明了一个不知道如何使用这2个下降箱转换多种货币的问题。
//jquery-3.6.0.min.js
const inputValue = Number(document.getElementById("inputVal").value);
console.log(selectedText);
console.log(selectedText2);
function getResult(){
const selectedText = $('#menuA :selected').text();
const selectedText2 = $('#menuB :selected').text();
let result;
if (selectedText === 'option1' && selectedText2 === 'option1'){
result = inputValue * 1;
console.log(result);
} else if (selectedText === 'option1' && selectedText2 === 'option2'){
result = inputValue * 0.1;
console.log(result);
} else if (selectedText === 'option1' && selectedText2 === 'option3'){
result = inputValue * 0.01;
console.log(result);
} else if (selectedText === 'option2' && selectedText2 === 'option1'){
result = inputValue * 5;
console.log(result);
}
document.getElementById('resultbox').value = result;
}
<form>
<input name="number" type="number" />
<select name="first" id="first">
<option value="1">EUR</option>
<option value="1.06">USD</option>
<option value="1.37">CAD</option>
</select>
<select name="second" id="second">
<option value="1">EUR</option>
<option value="1.06">USD</option>
<option value="1.37">CAD</option>
</select>
</form>
<div id="resultBox"></div>
Recently I started a Javascript course, so I am still figuring out how to do basic things best. My goal is to have 2 select menu's, both with the same currencies and their value, and convert them between eachother.
For example if EUR from menu A is combined EUR from menu B it will be 1 * 1. USD will be 1 * 1.06, and so on but this only allows me to convert from euro to something else.
I tried to figure out if this was possible with a loop. Would that somehow work?
Edit:
In the HTML, given by mstephen19, I show the issue of not knowing how to use these 2 dropboxes to convert multiple currencies.
//jquery-3.6.0.min.js
const inputValue = Number(document.getElementById("inputVal").value);
console.log(selectedText);
console.log(selectedText2);
function getResult(){
const selectedText = $('#menuA :selected').text();
const selectedText2 = $('#menuB :selected').text();
let result;
if (selectedText === 'option1' && selectedText2 === 'option1'){
result = inputValue * 1;
console.log(result);
} else if (selectedText === 'option1' && selectedText2 === 'option2'){
result = inputValue * 0.1;
console.log(result);
} else if (selectedText === 'option1' && selectedText2 === 'option3'){
result = inputValue * 0.01;
console.log(result);
} else if (selectedText === 'option2' && selectedText2 === 'option1'){
result = inputValue * 5;
console.log(result);
}
document.getElementById('resultbox').value = result;
}
<form>
<input name="number" type="number" />
<select name="first" id="first">
<option value="1">EUR</option>
<option value="1.06">USD</option>
<option value="1.37">CAD</option>
</select>
<select name="second" id="second">
<option value="1">EUR</option>
<option value="1.06">USD</option>
<option value="1.37">CAD</option>
</select>
</form>
<div id="resultBox"></div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定如何计算这些乘数(
1,5,0.5等)做一堆If ...其他语句。
I'm not sure how you're calculating these multipliers (
1, 5, 0.5, etc.
), but I think you should use data-attributes/value attributes and calculate your multiplier using those rather than doing a bunch of if...else statements.