1 回答

TA貢獻1788條經(jīng)驗 獲得超4個贊
把它剪短了一點。還有其他問題?!皹撕灐奔僭O(shè)是指標記元素的 id?!皹藴省焙汀把b備”選項實際上是單選按鈕,而不是復(fù)選框,因為大概您可以擁有其他選項之一。我重命名了一些類和 id,盡管您可能不需要所有這些。我假設(shè)您可能正在動態(tài)生成 HTML 并根據(jù)您擁有的行數(shù)為后綴添加“i”的值。我實際上只是對第一行進行了更改,并重新編寫了 JS,以便為該行的輸入元素中的值提供項目的總數(shù)。不確定您是如何計算這些總數(shù)的,因為我認為 $("#multiplier") 甚至不在您的代碼中。
<--- HTML SECTION --->>
// obtained from $nosday = $_POST['nosday'];
<br>
// value is normally $nosday
<br>
<input type="hidden" value = "1.5" id = "multiplier">
<input type="text" value="5" id="nosday"><br><br>
// obtained from $count = mysqli_num_rows($result);
<br>
// value is normally $count;
<br>
<input type="hidden" value="2" id="countrows"><br><br>
// Assume checkbox is $qri and $qri = "qr"."$i";<br>
<label for="standard1">Standard</label>
<input type="radio" name="equipment" id ="standard1" class="quip qr1" value="125" checked><br><br>
<label for="equipped1">Equipped</label>
<input type="radio" name="equipment" id ="equipped1" class="quip qr1" value="225" ><br><br>
<label for="gps1">GPS</label>
<input type="checkbox" name="gps" id ="gps1" value="20" class ="qr1"><br><br>
<label for="booster1">booster</label>
<input type="checkbox" name="booster" id ="booster1" value="20" class ="qr1"><br><br>
<label for="tents1">One tent</label>
<input type="checkbox" name="tents" id ="tents1" class="tents qr1" value="60" checked><br><br>
<label for="twotents1">Two tents :</label>
<input type="checkbox" name="twotents" id ="twotents1" class="tent qr1" value="80"><br><br>
// assume id="#dailytotal" where dailytotal = "dailytotal"."$i";
<br>
Daily :<span id="dailytotal1"> </span><br><br>
// Assume id = "$lengthtotal" where $lengthtotal = "lengthtotal"."$i";
<br>
Total :<span id="lengthtotal1"> </span><br><br>
// Hidden element for daily rate to use in $_POST, shown for now
<br>
<input tyep="text" id="dailytot1" name="pricef1" value="">
<br>
JS 可能需要調(diào)整,因為我不知道你是如何生成 HTML 的。事實上,它應(yīng)該計算加載時所有“i”行的總數(shù),并且添加一些東西讓它計算“i”行的總數(shù)并不難。這應(yīng)該給你一些工作。
function Calc() {
//get the values of the selected options
var counter = $("#countrows").val();
let totals = [];
for (i = 1; i <= counter; i++) {
totals[i] = 0;
$.each($('.qr' + i + ':checked'), function() {
totals[i] += parseInt($(this).val());
});
console.log(totals[i]);
$('#dailytotal' + i).text('R' + totals[i] + '/day');
$('#lengthtotal' + i).text('R' + totals[i] * parseFloat($("#multiplier").val()) + '/day');
$('#dailytot' + i).val(totals[i]);
$('#lengthtot' + i).val(totals[i] * parseFloat($("#multiplier").val()));
}
}
Calc();
$("[class*=qr]").on("change", function(e) {
e.preventDefault();
Calc();
});
"[class*=qr]" 并不是最好的,因為 *=qr 實際上并不是那么具體,這取決于您的標記。此外,不清楚您是否在任何地方都使用了表單標簽。
- 1 回答
- 0 關(guān)注
- 153 瀏覽
添加回答
舉報