简体   繁体   English

javascript。 将变量传递到变量输入字段

[英]javascript. passing a variable to a variable input field

I have a price calculator with multiple input fields but the calculation remains the same. 我有一个包含多个输入字段的价格计算器,但计算结果保持不变。 What I have is this: 我所拥有的是:

function calcone() {
    calc(Number(document.calc.priceboxa.value),
         Number(document.calc.amountboxa.value),
         document.calc.answera.value,);
}

function calctwo() {
    calc(Number(document.calc.priceboxb.value),
         Number(document.calc.amountboxb.value),
         document.calc.answerb.value,);
}

function calc(price, amount, costbox) {
    var one = price / amount,
    costbox = one;
}

I can't get "costbox = one;" 我无法获得“ costbox = one;” to send the variable back to the answer* fields. 将变量发送回answer *字段。 What am I doing wrong? 我究竟做错了什么? I've searched and can't find an answer to this. 我已经搜索过,但找不到答案。

New code that works: 有效的新代码:

function calcone() {
    calc(Number(document.calc.priceboxa.value),
         Number(document.calc.amountboxa.value),
         document.calc.answera,);
}

function calctwo() {
    calc(Number(document.calc.priceboxb.value),
         Number(document.calc.amountboxb.value),
         document.calc.answerb,);
}

function calc(price, amount, costbox) {
    var one = price / amount,
    costbox.value = one;
}

When you pass document.calc.answerb.value into your function as costbox it's like writing an address on a piece of paper and saying costbox will know where to find document.calc.answerb.value 当您将document.calc.answerb.value作为costbox传递到函数中costbox ,就像在一张纸上写一个地址并说costbox将知道在哪里可以找到document.calc.answerb.value

So when you do: 因此,当您这样做时:

costbox = one;

All you've done is rewritten the address on that sheet of paper. 您所要做的就是将地址改写在那张纸上。 You haven't changed the value of document.calc.answerb.value which costbox used to point to; 您尚未更改costbox所指向的document.calc.answerb.value的值; instead, you just told costbox to point to something else. 相反,您只是告诉costbox指向其他内容。

So what you have to do is actually change what costbox is pointing to...not the costbox itself. 因此,您要做的实际上是更改costbox 指向的内容...而不是costbox本身。 The question now is how do you resolve this? 现在的问题是如何解决这个问题? The answer is that instead of changing the value of costbox, you change the properties of costbox. 答案是,您无需更改costbox的值,而是可以更改costbox的属性。

This means that instead of passing in 这意味着不要传递

document.calc.answerb.value

you pass in 你传递

document.calc.answerb

and do costbox.value = one 并做costbox.value = one

function calc(price, amount, costbox) {
    var one = price / amount,
    costbox.value = one;
}

Now instead of wiping that piece of paper clear of the address and writing down a new one, you're visiting the address and saying: "I want the value changed" 现在,您无需在地址上擦掉那张纸并写下新的纸,而是在访问该地址并说:“我想要更改值”

Your passing the value to the method not the element you need to do this: 您将值传递给方法而不是您需要这样做的元素:

function calcone() {
    calc(Number(document.calc.priceboxa.value), Number(document.calc.amountboxa.value));
}

function calc(price, amount) {
    var one = price / amount;
    document.calc.answera.value = one;
}

Or: 要么:

function calcone() {
    calc(Number(document.calc.priceboxa.value), Number(document.calc.amountboxa.value), document.calc.answera);
}

function calc(price, amount, answerbox) {
    var one = price / amount;
    answerbox.value = one;
}

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

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