简体   繁体   English

Java中的If-Else问题

[英]If-Else Issue In Javascript

I am doing a conditional statement in javascript. 我正在用javascript做条件语句。 When the user enters a value in the input box "Collect", its value together with the value from input box "CurBal" will perform an addition and put the answer in the input box named "Balance". 当用户在输入框“ Collect”中输入值时,其值与输入框“ CurBal”中的值将进行加法运算,并将答案放入名为“ Balance”的输入框中。

If the user enters a value in the input box "Deposit", its value will be substracted from the "CurBal" and the difference will also be reflected (supposedly) in the "Balance" 如果用户在输入框“存款”中输入一个值,则将从“ CurBal”中减去其值,并且该差异也将(应该)反映在“余额”中

I have this code: 我有以下代码:

var collect = parseFloat(document.getElementById("Collect").value) ||0;
var dep     = parseFloat(document.getElementById("Deposit").value) ||0;
var curr    = parseFloat(document.getElementById("CurBal").value) ||0;
var bal     = document.getElementById("Balance");

if (collect.value = true)
{
 bal.value = (collect + curr).toFixed(2)
 bal       = document.getElementById("Balance");
}

else if (dep.value = true)
{
 bal.value = (curr - dep).toFixed(2)
 bal       = document.getElementById("Balance");
} 

The HTML: HTML:

<tr>
 <td> &nbsp;Collections: </td>
 <td> &nbsp; <input onchange="addNumbers()" type="text" id="Collect" name="Collect" value="" /> </td>
</tr>

<tr>
 <td> &nbsp;Deposits:</td>
 <td> &nbsp;<input onchange="addNumbers()" type="text" id="Deposit" name="Deposit" value="" /> </td>
</tr>

<tr>
 <td> &nbsp;Current Balance: </td>
 <td> &nbsp;<input onchange="addNumbers()" type="text" id="CurBal" name= "CurBal" value="<?php echo $all['balance']; ?>" readonly="readOnly" /> </td>
</tr>

<tr>
 <td> &nbsp;Balance: </td>
 <td> &nbsp;<input type="text" id="Balance" name="Balance" value="" readonly="readOnly" /> </td>
</tr>

But, its just performing the first condition correctly. 但是,它只是正确地执行了第一个条件。 If I entered a value in the input box "Deposit", it isn't subtracting the value from "CurBal", its just copying the value from "CurBal". 如果我在“存款”输入框中输入了一个值,则不是从“ CurBal”中减去该值,而是从“ CurBal”中复制了该值。

What should be removed or added or changed? 应该删除,添加或更改哪些内容? Thank you for the help 感谢您的帮助

Additional info: The user can only EITHER enter a value to "Collect" OR "Balance". 附加信息:用户只能输入“收集”或“余额”的值。

EDITED PER COMMENTS BELOW 修改后的评论如下

// Collect is now a number
var collect = parseFloat(document.getElementById("Collect").value) ||0;

// dep is now a number
var dep     = parseFloat(document.getElementById("Deposit").value) ||0;

// Cur is now a number
var curr    = parseFloat(document.getElementById("CurBal").value) ||0;

// bal is now pointing to an element
var bal     = document.getElementById("Balance");


// What are you trying to compare here? That bal !== 0 perhaps?
// Collect is a number, so we can do the compare here.
if (collect !== 0)  {
 bal.value = (collect + curr).toFixed(2)


 // Dep, on the other hand, is pointing to an element
 // so we use the property here
 // The leading '+' is a quick way to turn the string value (dep.value is a string)
 // into a number.
} else if (+dep.value !== 0) {
 bal.value = (curr - dep).toFixed(2)
}

** Notes ** The line: **注释**该行:

if (collect !== 0)  {

can be written shorter as 可以写成更短

if (collect)  {

but I always recommend writing the most readable version that describes exactly what you mean. 但我始终建议编写最易读的版本,以准确描述您的意思。

You have this: 你有这个:

if (collect.value = true)
{
 bal.value = (collect + curr).toFixed(2)
 bal       = document.getElementById("Balance");
}

else if (dep.value == true)
{
 bal.value = (curr - dep).toFixed(2)
 bal       = document.getElementById("Balance");
} 

It sounds as though you need this: 听起来好像您需要这样:

if (collect.value = true)
{
 bal.value = (collect + curr).toFixed(2)
 bal       = document.getElementById("Balance");
}

if (dep.value == true)
{
 bal.value = (curr - dep).toFixed(2)
 bal       = document.getElementById("Balance");
}

You have an else if condition, so if collect.value is true, your other conditions won't run. 您具有else if条件,因此,如果collect.value为true,则其他条件将不会运行。

Of note - you also are performing assignment in your if statements, when in fact, you should be checking for equality. 值得注意的是-您还在if语句中执行赋值,实际上,您应该检查是否相等。 In fact, you don't even need to do that; 实际上,您甚至不需要这样做。 you can do: 你可以做:

if (collect.value)
{
...
}

if (dep.value)
{
...
} 

You are assigning true to collect.value in the first if test. 您在第一个if测试中将true分配给collect.value The value of an assignment expression is the value assigned; 赋值表达式的值是已赋值; in this case true . 在这种情况下为true Thus the first test will always succeed. 因此,第一个测试将始终成功。 You should use == or === for comparison tests. 您应该使用=====进行比较测试。 (The first operator will convert types if necessary; the === operator will only succeed if the current data types are already the same). (如果需要,第一个运算符将转换类型;仅当当前数据类型已经相同时, ===运算符才会成功)。

For boolean values, it's better to use if (collect.value) (etc.) rather than if (collect.value == true) . 对于布尔值,最好使用if (collect.value) (etc.)而不是if (collect.value == true)

you are missing double equal sign in if condition. 您缺少if条件的双等号。 try changing the condition. 尝试更改条件。

if (collect.value != 0)

else if (dep.value != 0)

Try this 尝试这个

var collect = parseFloat(document.getElementById("Collect").value) ||0;
var dep     = parseFloat(document.getElementById("Deposit").value) ||0;
var curr    = parseFloat(document.getElementById("CurBal").value) ||0;
var bal     = document.getElementById("Balance");

if (collect != 0)
{
 bal.value = (collect + curr).toFixed(2)
 bal       = document.getElementById("Balance");
}

else if (dep != 0)
{
 bal.value = (curr - dep).toFixed(2)
 bal       = document.getElementById("Balance");
}

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

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