【发布时间】:2019-11-12 08:04:54
【问题描述】:
我正在尝试创建一个表格,该表格累计了每天和天数的值,但是当我尝试重新创建 php while 语句的产品时,该语句获取所有行然后显示每一行的信息,表格不要单独工作。我试图让它在尽可能多的行中工作,同时尽量减少代码。
正在开发的小提琴在这里https://jsfiddle.net/shiataz12/oj51vfpm/111/。
假设表单是 php while 循环的一部分,其中每个表单都是数据库中的新行。 #nosday 是一个 $_POST 元素。所有输入都提交到另一个页面。
到目前为止,我尝试使用 eq(i) 按类或名称隔离元素以及 $('#formaddi' + i).children 作为我的函数的前缀。 I've tried changing a few of the checkboxes to radio buttons where i needed only one selected to reduce some of the code needed to do this, however I still need to disable onetent and twotent options when standard is selected, but it disables all of他们。
每个表单都应该单独工作,我也尝试使用诸如 ^ 之类的选择器来隔离 id 或 class,但它要么不起作用,要么它们都一起工作。任何建议或想法。理想情况下,一个 api 页面可提供最佳功能,以便一次以一种形式选择特定输入。
<input type="text" value="5" id="nosday"><br><br>
<input type="hidden" value="3" id="countrows"><br>
////////////////////////FIRST FORM/////////////////////////////
<br>
<form id="formaddi1" class="formadd1">
<label for="standardi1">Standard</label>
<input type="checkbox" name="packn1" id ="standardi1" class="pack1 standard1 qr1" value="1050" checked><br><br>
<label for="equippedi1">Equipped</label>
<input type="checkbox" name="packn1" id ="equippedi1" class="pack1 equipped1 qr1" value="0" ><br><br>
<label for="gpsi1">GPS</label>
<input type="checkbox" name="gpsn1" id ="gpsi1" value="20" class ="gps1 qr1"><br><br>
<label for="boosteri1">booster</label>
<input type="checkbox" name="boostern1" id ="boosteri1" value="20" class =" booster1 qr1"><br><br>
<label for="onetenti1">One tent</label>
<input type="checkbox" name="tentn1" id ="onetenti1" class="stent1 onetent1 qr1" value="1350" disabled><br><br>
<label for="twotenti1">Two tents :</label>
<input type="checkbox" name="tentn1" id ="twotenti1" class="stent1 twotent1 qr1" value="1450" disabled><br><br>
Daily :<span id="dailytotali1"> </span><br><br>
Total :<span id="lengthtotali1"> </span>
<br><br>
<br><input tyep="text" id="dailytoti1" name="pricefn11" value="">
<br><input tyep="text" id="lengthtoti1" name="pricefn21" value="">
<br><br><br>
</form>
////////////////////////SECOND FORM/////////////////////////////
<BR>
<form id="formaddi2" class="formadd2">
<label for="standardi2">Standard</label>
<input type="radio" name="packn2" id ="standardi2" class="quip2 standard2 qr2" value="125" checked><br><br>
<label for="equippedi2">Equipped</label>
<input type="radio" name="packn2" id ="equippedi2" class="quip2 equipped2 qr2" value="0" ><br><br>
<label for="gpsi2">GPS</label>
<input type="checkbox" name="gpsn2" id ="gpsi2" value="20" class ="gps2 qr2"><br><br>
<label for="boosteri2">booster</label>
<input type="checkbox" name="boostern2" id ="boosteri2" value="20" class ="booster2 qr2"><br><br>
<label for="onetenti2">One tent</label>
<input type="radio" name="tentn2" id ="onetenti2" class="stent2 onetent2 qr2" value="1350" disabled><br><br>
<label for="twotenti2">Two tents :</label>
<input type="radio" name="tentn2" id ="twotenti2" class="stent2 twotent2 qr2" value="1450" disabled><br><br>
Daily :<span id="dailytotali2"> </span><br><br>
Total :<span id="lengthtotali2"> </span><br><br>
<br><input tyep="text" id="dailytoti2" name="pricefn12" value="">
<br><input tyep="text" id="lengthtoti2" name="pricefn22" value="">
<br><br><br>
</form>
////////////////////////THIRD FORM/////////////////////////////
<form id="formaddi3" class="formadd3">
<br>
<label for="standardi3">Standard</label>
<input type="checkbox" name="packn3" id ="standardi3" class="quip3 standard3 qr3" value="1050" checked><br><br>
<label for="equippedi3">Equipped</label>
<input type="checkbox" name="packn3" id ="equippedi3" class="quip3 equipped3 qr3" value="0" ><br><br>
<label for="gpsi3">GPS</label>
<input type="checkbox" name="gpsn3" id ="gpsi3" value="20" class ="gps3 qr3"><br><br>
<label for="boosteri3">booster</label>
<input type="checkbox" name="boostern3" id ="boosteri3" value="20" class ="booster3 qr3"><br><br>
<label for="onetenti3">One tent</label>
<input type="checkbox" name="tentn3" id ="onetenti3" class="stent3 onetent3 qr3" value="1350" disabled><br><br>
<label for="twotenti3">Two tents :</label>
<input type="checkbox" name="tentn3" id ="twotenti3" class="stent3 twotent3 qr3" value="1450" disabled><br><br>
Daily :<span id="dailytotali3"> </span><br><br>
Total :<span id="lengthtotali3"> </span><br><br>
<br><input tyep="text" id="dailytoti3" name="pricefn13" value="">
<br><input tyep="text" id="lengthtoti3" name="pricefn23" value="">
<form>
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]);
$('#dailytotali' + i).text('R' + totals[i] + '/day');
$('#lengthtotali' + i).text('R' + totals[i] * parseFloat($("#nosday").val()) + '/day');
$('#dailytoti' + i).val(totals[i]);
$('#lengthtoti' + i).val(totals[i] * parseFloat($("#nosday").val()));
}
}
Calc();
$("[class*=qr]").on("change", function(e) {
e.preventDefault();
Calc();
});
function Equip()
{
var counter1 = $("#countrows").val();
for (i = 1; i <= counter1; i++) {
$('input.onetent'+ i).eq(i).prop("disabled", false);
$('input.twotent'+ i).eq(i).prop("disabled", false);
$('input.onetent'+ i).eq(i).prop("checked", true);
$('input.standard'+ i).eq(i).prop("checked", false);
}
}
function Standard()
{
var counter2 = $("#countrows").val();
for (i = 1; i <= counter2; i++) {
$('#formaddi'+ i).children('input.onetent'+ i).eq(i).prop("checked", false);
$('#formaddi'+ i).children('input.twotent'+ i).eq(i).prop("checked", false);
$('#formaddi'+ i).children('input.onetent'+ i).eq(i).prop("disabled", true);
$('#formaddi'+ i).children('input.twotent'+ i).eq(i).prop("disabled", true);
$('#formaddi'+ i).children('input.equipped'+ i).eq(i).prop("checked", false);
}
}
function twoTent()
{
var counter3 = $("#countrows").val();
for (i = 1; i <= counter3; i++) {
$('#formaddi'+ i).children('input.onetent'+ i).eq(i).prop("checked", false);
}
}
function oneTent()
{
var counter4 = $("#countrows").val();
for (i = 1; i <= counter4; i++) {
$('#formaddi'+ i).children('input.twotent'+ i).eq(i).prop("checked", false);
}
}
$("[id^=standardi]").click(function() {
Standard();
});
$("[id^=equippedi]").click(function() {
Equip();
});
$("[id^=onetenti]").click(function() {
oneTent();
});
$("[id^=twotenti]").click(function() {
twoTent();
【问题讨论】:
-
这是一个相当复杂的逻辑。您能否举例说明您点击了什么、会发生什么以及您希望发生什么?
-
@GaloisGirl 好吧,假设您已经看过 Fiddle。 FIRST FORM - 单击
input field where name is packn1和id = "equippedi1". **First Action** - **uncheck**id="standardi1"` 第二个操作 - 对所有 class="stent1" 的输入禁用 false b> Third Action - checkinputwhereid="onetenti1"当计算发生变化时,计算已经完成,所以没关系,这部分工作正常。 下一个功能 点击->id=''standardi1"->**如果选中则取消选中**id="equippedi1" - > **Disable true** ->class="stent1"` -> 如果选中则取消选中-> -
either class="stent1" -
基本上每个表单只能在
i迭代直到到达var counter = $("#countrows").val();时自行取消选中或禁用 -
用
$counter.each(function() {怎么样?
标签: jquery class for-loop input