javascript(数学)将数组相加

发布于 2024-11-27 09:47:46 字数 300 浏览 4 评论 0原文

我有两个 javascript 文本框。

<input type="text" name="test" value="300" />
<input type="text" name="test" value="500" />

如何使用 javascript 来提醒文本框中商品的总价?

像这样的东西会起作用吗?

var price = document.getElementById("test");
alert(price)

I have two javascript text boxes.

<input type="text" name="test" value="300" />
<input type="text" name="test" value="500" />

How can I use javascript to alert the total price of the items in the text box?

Would something like this work?

var price = document.getElementById("test");
alert(price)

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

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

发布评论

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

评论(4

白云不回头 2024-12-04 09:47:46

这将考虑所有 input 元素(如果有很多元素,这很有用)。按 type 过滤它们并在循环中获取它们的值:

var inputs = document.getElementsByTagName("input");
var total = 0;

for (var i = 0; i < inputs.length; i++){
    if (inputs[i].type = "text"){
        total += parseInt(inputs[i].value, 10);
    }
}

alert(total);

This will take all input elements into account (useful if you have many). Filter them by type and get their values in loop:

var inputs = document.getElementsByTagName("input");
var total = 0;

for (var i = 0; i < inputs.length; i++){
    if (inputs[i].type = "text"){
        total += parseInt(inputs[i].value, 10);
    }
}

alert(total);
神经大条 2024-12-04 09:47:46
<input id="test1" type="text" name="test" value="300" />
<input id="test2" type="text" name="test" value="500" />

JS:

var price = parseInt(document.getElementById("test1").value, 10) + parseInt(document.getElementById("test2").value, 10);
<input id="test1" type="text" name="test" value="300" />
<input id="test2" type="text" name="test" value="500" />

JS:

var price = parseInt(document.getElementById("test1").value, 10) + parseInt(document.getElementById("test2").value, 10);
东风软 2024-12-04 09:47:46
<input id="price1" type="text" name="test" value="300" />
<input id="price2" type="text" name="test" value="500" />

这将起作用:

var price = document.getElementById("price1").value+document.getElementById("price2").value;
alert(price)

注意不要有其他带有 id="price1"id="price2" 的标签

<input id="price1" type="text" name="test" value="300" />
<input id="price2" type="text" name="test" value="500" />

This will work:

var price = document.getElementById("price1").value+document.getElementById("price2").value;
alert(price)

Note Do not have other tags with id="price1" or id="price2"

一个人练习一个人 2024-12-04 09:47:46

由于多种原因,您所写的内容不会起作用。首先,顾名思义,getElementById 根据 id 属性的值获取元素。您还没有为您的 input 元素提供 id 属性,因此这不起作用。

其次,document.getElementById('someId') 返回一个元素,但您需要该值。您可以使用 value 属性来获取它:

var price1 = parseInt(document.getElementById("test1").value, 10);
var price2 = parseInt(document.getElementById("test2").value, 10);
alert(price1 + price2);

这将适用于以下 HTML:

<input type="text" name="test" id="test1" value="300" />
<input type="text" name="test" id="test2" value="500" />

请注意 parseInt 的使用。 value 属性返回一个字符串,因此我们使用 parseInt 尝试将其解析为数字。如果您不使用它,price1 + Price2 将简单地连接字符串。

What you have written will not work, for several reasons. Firstly, as the name suggests, getElementById gets an element based on the value of the id attribute. You haven't given your input elements an id attribute, so that's not going to work.

Secondly, document.getElementById('someId') returns an element, but you want the value. You can use the value property to get it:

var price1 = parseInt(document.getElementById("test1").value, 10);
var price2 = parseInt(document.getElementById("test2").value, 10);
alert(price1 + price2);

This will work with the following HTML:

<input type="text" name="test" id="test1" value="300" />
<input type="text" name="test" id="test2" value="500" />

Note the use of parseInt. The value property returns a String, so we use parseInt to attempt to parse that into a Number. If you don't use it, price1 + price2 will simply concatenate the strings.

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