JavaScript 中的货币转换器出现问题

发布于 2024-12-21 18:36:47 字数 1405 浏览 1 评论 0原文

我在使用这个 javascript 时遇到了问题。这是一个示例

 window.onload = initPage;

var euro;
var convert;

function initPage()
{
    document.getElementById("convertButton").onclick = calcAnswer();
    document.getElementById("conversionType").onchange = calcAnswer();
}

function calcAnswer()
{
    //alert(document.getElementById("conversionType").value);

    var value1 = document.getElementById("amount").value;
    var conversionType = document.getElementById("conversionType").value;
    //alert(conversionType);

    if(var value = document.getElementById("conversionType").value=="polish");
        document.getElementById("answer").value=(value1-32)/9*5;
    else
        document.getElementById("answer").value=value1*9/5+32;
}

,这是 html

  <h1>Currency Converter</h1>


  <form name="convert">

Choose which currency you would like to convert to the Euro:
<select id="conversionType">
    <option value="polish">Polish Zloty</option>
    <option value="ukraine">Ukraine Hryvnia</option>
</select>
</br>
</br>

<hr>
      Amount:<input id="amount" type="text" />
<input id="convertButton" type="button" value="Convert->"/>
      To:
<input id="answer" type="text" name="answer" readonly="readonly"/>


</form>

,我使用旧的温度转换器,并且没有更改该部分,但即使该部分也无法工作。

Im having trouble with this javascript. here is a n example

 window.onload = initPage;

var euro;
var convert;

function initPage()
{
    document.getElementById("convertButton").onclick = calcAnswer();
    document.getElementById("conversionType").onchange = calcAnswer();
}

function calcAnswer()
{
    //alert(document.getElementById("conversionType").value);

    var value1 = document.getElementById("amount").value;
    var conversionType = document.getElementById("conversionType").value;
    //alert(conversionType);

    if(var value = document.getElementById("conversionType").value=="polish");
        document.getElementById("answer").value=(value1-32)/9*5;
    else
        document.getElementById("answer").value=value1*9/5+32;
}

here is the html

  <h1>Currency Converter</h1>


  <form name="convert">

Choose which currency you would like to convert to the Euro:
<select id="conversionType">
    <option value="polish">Polish Zloty</option>
    <option value="ukraine">Ukraine Hryvnia</option>
</select>
</br>
</br>

<hr>
      Amount:<input id="amount" type="text" />
<input id="convertButton" type="button" value="Convert->"/>
      To:
<input id="answer" type="text" name="answer" readonly="readonly"/>


</form>

im using an old temperature converter and havent changed that part part but even that part is not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亣腦蒛氧 2024-12-28 18:36:47

对于初学者来说,这两行是错误的:

document.getElementById("convertButton").onclick = calcAnswer();
document.getElementById("conversionType").onchange = calcAnswer();

将它们更改为:

document.getElementById("convertButton").onclick = calcAnswer;
document.getElementById("conversionType").onchange = calcAnswer;

您想要为 onclickonchange 分配函数引用,而不是实际调用该函数并分配返回值。

然后,修复 calcAnswer 中的 if 语句,如下所示:

function calcAnswer()
{
    var amount = document.getElementById("amount").value;
    var conversionType = document.getElementById("conversionType").value;
    var answerElement = document.getElementById("answer");
    //alert(conversionType);

    if(conversionType == "polish") {
        answerElement.value = (amount-32)/9*5;
    } else {
        answerElement.value = amount*9/5+32;
    }
}

For starters, these two lines are wrong:

document.getElementById("convertButton").onclick = calcAnswer();
document.getElementById("conversionType").onchange = calcAnswer();

Change them to:

document.getElementById("convertButton").onclick = calcAnswer;
document.getElementById("conversionType").onchange = calcAnswer;

You want to assign a function reference to onclick and onchange, not actually call the function and assign the return value.

Then, fix the if statement in calcAnswer like this:

function calcAnswer()
{
    var amount = document.getElementById("amount").value;
    var conversionType = document.getElementById("conversionType").value;
    var answerElement = document.getElementById("answer");
    //alert(conversionType);

    if(conversionType == "polish") {
        answerElement.value = (amount-32)/9*5;
    } else {
        answerElement.value = amount*9/5+32;
    }
}
柒七 2024-12-28 18:36:47

应该是

document.getElementById("convertButton").onclick = calcAnswer;
document.getElementById("conversionType").onchange = calcAnswer;

(没有括号)

您只需要引用该函数,而不是执行它。

Should be

document.getElementById("convertButton").onclick = calcAnswer;
document.getElementById("conversionType").onchange = calcAnswer;

(without the parens)

You just need to reference the function, not execute it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文