1

我需要编写一个 jQuery / Java 函数,该函数将从 AJAX 更新值中获取数字,对其进行清理以删除 $,将价格降低一定百分比(discPerc),然后制作一个警报窗口,通知客户减少了价格。

这是我到目前为止所拥有的,我意识到我不是最好的编码员!

<head>
<script type="text/javascript">
function superSalePrice(discPerc) {
      var optionPrice = $("#productTotal").val();
  var applyDisc = optionPrice * discPerc;
  var rPrice = Math.round(applyDisc * 1) / 1;

  $("tB").click(function() {
  alert("With selected options, final price with discounts is $" + rPrice + "!");
  };
  )
};
</script>
</head>

//THEN the button
<input type="button" id="tB" value="Test Disc. Calc." onclick="superSalePrice(.85);" />

//THEN the option
<td id="productTotal" class="rowElement3"> $3,450.00</td>

我不知道如何清理该值,因此尚未包含该部分。

谢谢!

4

2 回答 2

2

要清理您的号码,只需使用正则表达式删除不是数字或点的所有内容:

>'   $3,450.00 '.replace(/[^0-9.]/g, '');
'3450.00'

这是我构建 JavaScript 的方式

function superSalePrice(discount) {
  var price = $("#productTotal").text();
  price = parseFloat(price.replace(/[^0-9.]/g, ''));
  price *= discount;

  return Math.round(100 * price) / 100;
)

$('#tB').click(function() {
  var total = superSalePrice(0.85);
  alert("With selected options, final price with discounts is $" + total + "!");
};

和 HTML:

<input type="button" id="tB" value="Test Disc. Calc." />

<td id="productTotal" class="rowElement3">$3,450.00</td>

此外,您的 HTML 无效。<td>并且<input>元素不能是兄弟。

于 2012-09-30T04:50:44.293 回答
0

我认为您需要进行一些更改,请尝试以下操作:

function superSalePrice(discPerc) {
    var optionPrice = $("#productTotal").html(); // Changed from .val()
    var total = parseFloat(optionPrice.replace(/\$/g, "").replace(/,/g, ""));  // Added this
    var applyDisc = optionPrice * discPerc;
    var rPrice = Math.round(applyDisc * 1) / 1;  // Didn't check this

    // I'm not sure what the following is for, because you are just binding ANOTHER function every time the button is clicked. Maybe you just want the "alert" line
    $("tB").click(function() {
        alert("With selected options, final price with discounts is $" + rPrice + "!");
    });
}

阅读评论!

于 2012-09-30T04:46:06.913 回答