javascript - 应该有效的多个 Javascript If 语句

标签 javascript jquery

我正在处理发票项目和零件部分,我有公司价格和客户价格。

具体情况如下:

  • 如果零件价格为 14.99,则固定费用为 14.95
  • 如果零件价格为 549.99,则为 4* 价格

第一个 if 语句有效,但第二个语句无效,有什么想法吗?

var five = 4.99;
var fifty = 49.99;
var four = 4;
var fourteenninefive = 14.95;

var qty4 = h.find('[name="requestpartquantity[]"]');
var price4 = h.find('[name="requestpartdb[]"]');
var total4 = h.find('[name="requestcustomerpartdb[]"]');

if (price4 < five ) {

  total4.val(qty4.val() * fourteenninefive);

} else if (price4 <= fifty ) {

  total4.val(qty4.val() * price4.val() * four);

} else {

}

我尝试了您的建议,但仍然无效,请参见下面的屏幕截图:

total4.val((+qty4.val()) * (+price4.val()) * four);

enter image description here

如果零件价格为 10,则意味着客户总数应为 80

10(Price) * 2(quantity) * 4 = 80

最佳答案

假设您从某些 <input> 获取价格与name属性,您应该将它们转换为整数(这就是我的代码中的 + 所做的)。另外,你忘记使用 .val()方法price4 .

var five = 4.99;
var fifty = 49.99;
var four = 4;
var fourteenninefive = 14.95;

var qty4 = h.find('[name="requestpartquantity[]"]');
var price4 = h.find('[name="requestpartdb[]"]');
var total4 = h.find('[name="requestcustomerpartdb[]"]');

if (+price4.val() < five ) {

  total4.val(+qty4.val() * fourteenninefive);

} else if (+price4.val() <= fifty ) {

  total4.val((+qty4.val()) * (+price4.val()) * four);

} else {

}

更具可读性的替代方案是使用内置 parseInt()功能。

关于javascript - 应该有效的多个 Javascript If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41065661/

相关文章:

javascript - 如何在 Chrome 开发者工具中使用 jQuery DataTables API?

在while循环中工作的php jquery弹出消息框

javascript - 将 ID 传递到 Bootstrap Modal 中?

javascript - 提示框不工作

javascript - IndexedDB索引概念?

javascript - React hooks 无法通过动态命名正确更新状态

javascript - 第一次使用 redux。如何实现?

jQuery 选择器点击功能

javascript - 跨度 jQuery 的总和

javascript - JQuery 检查是否需要输入