我对 javascript 很陌生,我正在尝试输入售价的 50% 的最大值,请指教
我有两个字段“sale_price”和“discount”
JavaScript如何限制和改变折扣标称输入,使其不超过售价的50%
您可以在此处使用 if / else 语句:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else
基本上说
if(sale_price < (normal_price * 0.5)) {
var something = (normal_price * 0.5)
} else {var something = sale_price}
或者我更喜欢下面链接的三元更好的路线 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator
sale_price < (normal_price * 0.5)?
sale_price = (normal_price * 0.5):
sale_price
您需要根据 sale_price 的变量类型来选择方法。如果它是一个常量,您将需要创建一个不同的变量,我将使用 if / else 语句。如果它是可变的,你最好使用三元。
希望这有帮助!