它只显示if语句。没有达到第二或第三个声明。 HTML部分仅显示“commentE”值。
var totalAmount = (conversion * sAmount);
totalAmount = $("#totalAmount").val(totalAmount);
var myadvice;
myadvice = $("#myadvice").val();
var great;
great = localStorage.getItem("great");
var commentE;
commentE = localStorage.getItem("commentE");
var between;
between = localStorage.getItem("between");
var betweenb;
betweenb = localStorage.getItem("betweenb");
var commentB;
commentB = localStorage.getItem("commentB");
var less;
less = localStorage.getItem("less");
var commentL;
commentL = localStorage.getItem("commentL");
if (totalAmount >= great) {
myadvice = $("#myadvice").val(commentE);
} else if ((totalAmount >= between) && (totalAmount <= betweenb)) {
myadvice = $("#myadvice").val(commentB);
} else if (totalAmount <= less) {
myadvice = $("#myadvice").val(commentL);
这些是HTML部分(获取输入的索引页)
<div data-role="navbar">
<ul>
<li>
<label for="between">Between</label>
<div class="ui-grid-a">
<div class="ui-block-a">
<input type="text"
name="between" id="between" placeholder="between" value="200">
</div>
<div class="ui-block-b">
<input type="text"
name="betweenb" id="betweenb" placeholder="between" value="499">
</div>
</div>
</li>
<li>
<label for="commentB">Comment</label>
<input type="text" name="commentB"
id="commentB" placeholder="comments" value="OK">
</li>
</ul>
</div>
这些应该是结果(结果页面显示上一页的输入
<div data-role="main" class="ui-content">
<div class="ui-field-contain">
<div data-role="fieldcontainer">
<label for="advice"> My Advice is as follows: </label>
<input type="text" name="advice" id="advice">
</div>
</div>
</div>
</b>
答案 0 :(得分:1)
如果else语句为executed in order,例如:
total = 10;
greater = 5;
between = 4;
if(total >= greater) {
console.log("comment a");
} else if (total >= between) {
console.log("comment b");
}
结果将为comment a
,因为在total >= greater
之前尝试了total >= between
。这两个语句都是正确的,但第一个语句是首先尝试的,并且只执行一个语句的内容。运行if else
语句中的代码后,执行的下一个操作将在if else
组之下。
答案 1 :(得分:0)
你的代码就是各种各样的错误。
totalAmount = $("#totalAmount").val(totalAmount);
这将totalAmount设置为jQuery对象$("#totalAmount")
您可能希望将其设置为数字。
great = localStorage.getItem("great");
这对字符串很有帮助。也许"5"
。
您可能想调用parseFloat将其解析为数字。
//so
if (totalAmount >= great) {
//means
if ($("#totalAmount") > "5") {
jQuery对象是否大于字符串?显然这是真的!所以你得到第一个if。