简体   繁体   中英

Javascript - 2 statements for one condition

I'm trying to allow my prices to display under 2 conditions

  1. sales price must be less than the base price
  2. "don't show pricing is unchecked" (yes or no) in our system

     var basPrc = "$5000"; var onlnPrc = "<%=getAttribute('item','382798','salesprice')%>"; var CallForPrice = "<%=getAttribute('item','382798','dontshowprice')%>"; if (onlnPrc < basPrc || CallForPrice == "No") { document.write('<span class="strike">Base Price: <span>'+basPrc+'</span></span>') document.write("<br /><strong class=\\"saleprice\\">Our Price: <%=getAttribute('item','382798','salesprice')%></strong><br />"); //savings var savings = onlnPrc - basPrc; document.write ('<span class="save">You Save: <span class="red">'+ savings +'</span></span><br />'); } //if don't show pricing is checked if (CallForPrice = "Yes") { var basPrc = null; var onlnPrc = null; document.write('<br /><strong class="saleprice">Call For Pricing<strong><br />'); } //if no online pricing else {document.write('<br /><strong class="saleprice">Our Price: '+basPrc+' <strong><br />');} 

I tried the "&&" operators and no luck, any idea on what I should do next?

Your basPrc is a String, not a Number; you should initialize it to 5000 , not "$5000" (the lack of quotes being important here). I'm not sure at all what onlnPrc will be. You need to make sure both are numbers. Otherwise, when you do basPrc > onlnPrc you will be doing a String comparison, not a numeric one.

// Base Price defaults to 5000
var basPrc = 5000;

// Parse the Online Price as a floating point number;
// if the result is NaN, default it to 0
var onlnPrc = parseFloat("<%=getAttribute('item','382798','salesprice')%>") || 0;

You should endeavor to make sure that basPrc and onlnPrc are always numbers since you are doing calculations with them. Leave the display of currency symbols or decimal points to the pieces of the code where you actually display the data.

Unrelated question: Where does this code live? What's it for? I've never seen NetSuite code that looked anything like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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