简体   繁体   English

引导卡上的Javascript代码排序不正确

[英]Javascript code on bootstrap cards dosen't sort right

So I have some javascript code which is applied to a page with lots of contracts. 所以我有一些适用于具有大量合同的页面的javascript代码。 All these contracts are inside bootstrap cards, and the price for the contract is on the h1 tag on each card. 所有这些合同都在引导卡中,并且合同的价格在每张卡的h1标签上。 But my code is sorting wrong and I don't know how, it's always coming up with the wrong answer. 但是我的代码排序错误,我也不知道如何,它总是会给出错误的答案。 All i want to do is find the cheapest contract-card and put a green border around it. 我要做的就是找到最便宜的合同卡,并在其周围放一个绿色边框。

function checkChangeCheapestID(){
 //get all the price form the Cards
 //set the cheapest border to green for cheapest
 var amountOfCards = document.getElementsByClassName("card");
 var cheapestCard = 0;
 var cheapNum = 0;

 for (var i = 0; i<amountOfCards.length; i++){
  var cardTitleNow = document.getElementsByClassName("box-top-value")[i].innerHTML;

  if(i == 0){
    cheapestCard = cardTitleNow;
    cheapNum = i;
    //if there's no past reffrence of cheapest price then, it sets the one it gets to as the cheapest
  } else if (cardTitleNow<cheapestCard){
    cheapestCard = cardTitleNow;
    cheapNum = i;
 }
}

 var choosenCard = amountOfCards[cheapNum];
 choosenCard.style.boxShadow= "0 16px 14px green";
}
checkChangeCheapestID();

The prices on the cards are generated from a database through php. 卡上的价格是通过php从数据库生成的。

Can someone help me figure out where it goes wrong? 有人可以帮我弄清楚哪里出了问题吗?

Here's the html code, php is used through a framework called laravel. 这是html代码,php通过称为laravel的框架使用。

<div class="card-body">

                        <div class="box-1-item">
                            <h6>{{($duration/12).' year/s contract'}}</h6>
                            <div class="box-top">

                                <p class="box-top-unit">£</p>
                                <h1 class="box-top-value">{{round((($years['annual']['amount'])/100),1,PHP_ROUND_HALF_UP)}}</h1>
                            </div>
                            <p class="box-top-dec">per Year</p>
                        </div>

                        <div class="box-2-items">
                            <div class="box-2-items-left">
                                <h5 class="box-2-items-left-text">Unit charges</h5>
                                <div class="box-2-items-left-value">
                                    <div class="box-2-items-left-value-val">
                                          <p>{{round(($years['unit_charge']['charge']),2,PHP_ROUND_HALF_UP)}}</p>
                                    </div>
                                    <p class="box-2-items-left-value-unit">{{'p/'.($years['unit_charge']['unit'])}}</p>
                                </div>
                            </div>
                            <div class="box-2-items-right">
                                <h5 class="box-2-items-left-text">Standing charges</h5>
                                <div class="box-2-items-left-value">
                                    <p class="box-2-items-left-value-val">{{round(($years['standing_charge']['charge']),2,PHP_ROUND_HALF_UP)}}</p>
                                    <p class="box-2-items-left-value-unit">{{'p/'.($years['standing_charge']['unit'])}}</p>
                                </div>
                            </div>
                        </div>

                        <div class="box-2-items">
                            <div class="box-2-items-left">
                                <h5 class="box-2-items-left-text">Start Date:</h5>
                                <div class="box-2-items-dates">
                                      <p>{{$quotes['start_date']->format('d/m/Y')}}</p>
                                </div>
                            </div>
                            <div class="box-2-items-right">
                                <h5 class="box-2-items-left-text">End Date:</h5>
                                <div class="box-2-items-dates">
                                      <p>{{$quotes['start_date']->copy()->addMonths($duration)->format('d/m/Y')}}</p>
                                </div>
                            </div>
                        </div>

                    </div>

Your problem is this line: 您的问题是这一行:

if (i == 0) 

This will always be true the first time, so it should be this: 第一次总是如此,所以应该这样:

if (cardTitleNow == 0)

So apparently it was treating is as a string. 因此,显然它被视为字符串。 There for each char was treated as the sorting through instead of the whole number. 每个字符在那里被视为排序而不是整数。 So 111 would be lower than 9 because 1 is less than 9. 因此111将小于9,因为1小于9。

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

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