简体   繁体   English

Javascript-一种情况的2条陈述

[英]Javascript - 2 statements for one condition

I'm trying to allow my prices to display under 2 conditions 我正在尝试让我的价格在2种情况下显示

  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; 您的basPrc是字符串,而不是数字; you should initialize it to 5000 , not "$5000" (the lack of quotes being important here). 您应该将其初始化为5000 ,而不是"$5000" (此处缺少引号很重要)。 I'm not sure at all what onlnPrc will be. 我完全不确定onlnPrc是什么。 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. 否则,当您执行basPrc > onlnPrc ,将进行字符串比较,而不是数字比较。

// 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. 您应该尽力确保basPrconlnPrc始终是数字,因为您正在使用它们进行计算。 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. 我从未见过NetSuite代码看起来像这样。

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

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