我需要编写一个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>
我不知道如何消毒该值,因此该部分尚未包括在内。
谢谢!
答案 0 :(得分: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>
元素不能是兄弟姐妹。
答案 1 :(得分: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 + "!");
});
}
阅读评论!