简体   繁体   English

Javascript + =等价?

[英]Javascript += equivalent?

My code is a simple function that checks which radio button was pressed, and adds the value of that radio button to my var input = 0; 我的代码是一个简单的函数,它检查按下了哪个单选按钮,并将该单选按钮的值添加到我的var input = 0; . However, I know I am doing something wrong as it works but the output is wrong. 但是,我知道我做错了,因为它有效,但输出错误。 When one of the if statements is true, instead of input(0) now being equal to itself plus the new value of the getElementById("small").value , it prints out 010 as opposed to the now 10 . 当其中一个if语句为真时,而不是输入(0)现在等于它本身加上getElementById("small").value ,它打印出010而不是现在的10

I know in Java there was a convention similar to input += getElementById("small").value; 我知道在Java中有一个类似于input += getElementById("small").value;的约定input += getElementById("small").value; but this doesn't seem to work. 但这似乎不起作用。 So as you can see in my example below, I tried the alternative of input = input + /*code*/; 因此,正如您在下面的示例中所看到的,我尝试了input = input + /*code*/;的替代方法input = input + /*code*/; but still no luck. 但仍然没有运气。

I am new to JavaScript but very familiar with Java. 我是JavaScript的新手,但对Java非常熟悉。 I imagine I'm just using the wrong syntax here but all my Google searches are a bust. 我想我在这里只是使用了错误的语法,但我所有的Google搜索都是破产。

function calculate()
{
    var input = 0;
    if (document.getElementById("small").checked) 
    {
        input = input + document.getElementById("small").value;
    }
    else if (document.getElementById("medium").checked)
    {
        input = input + document.getElementById("medium").value;
    }
    else if (document.getElementById("large").checked)
    {
        input = input + document.getElementById("large").value;
    }
    else
    {
        alert("failed");
    }

    document.getElementById("outar").innerHTML = input;
}

You're trying to do arithmetic with a string. 你正试图用字符串算术。 You need to use parseInt() around each of your document.getElementById("....").value expressions because the .value property is a string. 您需要在每个document.getElementById("....").value表达式周围使用parseInt() ,因为.value属性是一个字符串。

Example: 例:

input += parseInt(document.getElementById("small").value);

Your value is not a number by default, it's a string. 默认情况下,您的值不是数字,而是字符串。 You got to parse it first: 你必须先解析它:

input += parseInt(document.getElementById("large").value);

您可以在字符串值之前添加额外的+符号,以便将其转换为数字:

input += +document.getElementById("large").value;

You need to convert .value to a number as an integer or a float with parseInt or parseFloat . 您需要使用parseIntparseFloat.value转换为整数或浮点数。 .value is a string. .value是一个字符串。

Javascript is dynamically typed language, not static typed like java. Javascript是动态类型语言,不像java那样是静态类型。 document.getElementById("small").value returns a string which needs to be converted to int with parseInt(). document.getElementById(“small”)。value返回一个字符串,需要使用parseInt()将其转换为int。

Ho[e that helps :) 何[有帮助:)

You can also refer to this link- 你也可以参考这个链接 -

The returned value from the value is a string . 值的返回值是一个字符串。 You have to convert it to int before doing any arithmetic operations. 在进行任何算术运算之前,必须将其转换为int。

JavaScript is a dynamically typed language, not static typed like java. JavaScript是一种动态类型语言,不像java那样是静态类型的。

Return Value : A String, representing the value of the text field 返回值 :一个String,表示文本字段的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM