<head>
<meta charset="utf-8">
<title></title>
<script src="js/jquery-2.1.1.js"></script>
</head>
<body>
样品数量<input id="num" />
<table border="1">
<thead>
<tr>
<td>项目</td>
<td>规格上限</td>
<td>规格下限</td>
<td id="num1">量测数据</td>
<td>pass/fail</td>
<td><button onclick="addtr()">+</button><button onclick="deltr()">-</button></td>
</tr>
</thead>
<tbody id="body">
</tbody>
</table>
<button id="btn">aa</button>
</body>
function addtr() {
if ($("#num").val()) {
$("#num1").attr("colspan",$("#num").val());
let temp = "";
temp += "<tr name='info'>"
temp += "<td><input name='project'></td>"
temp += "<td><input name='normsUplimit'></td>"
temp += "<td><input name='normsDownlimit'></td>"
for (let i = 0; i < parseInt($("#num").val()); i++) {
temp += "<td><input name='normsData'></td>"
}
temp += "<td name='ifAdopt'></td>"
temp += "</tr>"
$("#body").append(temp);
} else {
alert('请先填写样品数量');
}
}
function deltr() {
if ($("[name='info']").eq(0).html()) {
$("[name='info']").eq(0).remove();
}else{
alert("请先添加表单信息");
}
}
function tbodyreturnjson(infoName, projectName, normsUplimitName, normsDownlimitName, normsDataName, ifAdoptName) {
let projectinfos = [];
let normsUplimitinfos = [];
let normsDownlimitinfos = [];
let normsDatainfos = [];
let ifAdoptinfos = [];
for (let i = 0; i < $(projectName).length; i++) {
projectinfos.push($(projectName).eq(i).val());
normsUplimitinfos.push($(normsUplimitName).eq(i).val());
normsDownlimitinfos.push($(normsDownlimitName).eq(i).val());
ifAdoptinfos.push($(ifAdoptName).eq(i).text());
let normsDatas = "";
let a = $(infoName).eq(i).find(normsDataName).length;
for (let j = 0; j < a; j++) {
normsDatas += $(infoName).eq(i).find(normsDataName).eq(j).val() + ";";
}
normsDatainfos.push(normsDatas);
}
let arrjsoninfo = [];
for (let i = 0; i < projectinfos.length; i++) {
let jsoninfo = {
itemNo: "",
project: projectinfos[i],
normsUplimit: normsUplimitinfos[i],
normsDownlimit: normsDownlimitinfos[i],
normsData: normsDatainfos[i],
ifAdopt: ifAdoptinfos[i],
recUser: "",
};
arrjsoninfo.push(jsoninfo);
}
return arrjsoninfo;
}
$("#btn").click(function() {
ifpassorfail("[name='info']", "[name='normsUplimit']", "[name='normsDownlimit']", "[name='normsData']",
"[name='ifAdopt']");
console.log(tbodyreturnjson("[name='info']", "[name='project']", "[name='normsUplimit']",
"[name='normsDownlimit']", "[name='normsData']", "[name='ifAdopt']"));
})
function ifpassorfail(infoName, normsUplimitName, normsDownlimitName, normsDataName, ifAdoptName) {
for (let i = 0; i < $(normsUplimitName).length; i++) {
let normsUplimit = $(normsUplimitName).eq(i).val();
let normsDownlimit = $(normsDownlimitName).eq(i).val();
let passnum = 0;
for (let j = 0; j < $(infoName).eq(i).find(normsDataName).length; j++) {
letnormsData = $(infoName).eq(i).find(normsDataName).eq(j).val();
if (parseInt(normsUplimit) > parseInt(letnormsData) && parseInt(normsDownlimit) < parseInt(letnormsData)) {
passnum++;
}
if (passnum == $(infoName).eq(i).find(normsDataName).length) {
$(infoName).eq(i).find(ifAdoptName).text("pass");
} else {
$(infoName).eq(i).find(ifAdoptName).text("fail");
}
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/79031bc9e4ab5d018b0583692585debf.jpeg)