我想從數字中減去一個百分比,並在使用jQuery的輸入中顯示,但表單使用PHP/MySQL拉入值。使用PHP/MySQL值的jQuery數學計算
我使用的是選擇欄額外的屬性,因爲該值將被回發到數據庫的不同表的唯一ID,這是我有:
<input type="text" id="invamount" name="invamount" value="250.00"/>
<select id="invdiscount">
<option data-discount="0" value="" selected>No Discount</option>
<option data-discount="5" value="1">Discount 1 - 5%</option>
<option data-discount="10" value="2">Discount 2 - 10%</option>
<option data-discount="15" value="3">Discount 3 - 15%</option>
</select>
<input type="text" id="invtotal" name="invtotal" />
<script>
jQuery(document).ready(function() {
jQuery("#invtotal").on("click", function() {
sum();
});
});
function sum() {
var amount = document.getElementById('invamount').value;
var discount = jQuery("#invdiscount option").attr("data-discount");
var maths1 = (discount * amount/100);
document.getElementById('invtotal').value = maths1;
}
</script>
有沒有在控制檯錯誤加載窗體時,但每次我選擇一個不同的打折時間,總輸入的值只是停留在0
你不能讓PHP在選項標記中加入額外的屬性嗎? (如數據折扣=「」),並閱讀與jQuery的價值? – Lexib0y
好主意,讓我試試吧。 – Adrian
僅供參考,您可以隨時製作額外的屬性,從HTML5它是官方支持的命名約定,即自定義屬性應該以數據開始他們的名字 – Lexib0y