【发布时间】:2019-04-02 20:35:36
【问题描述】:
我有一个 HTML 表格,里面有一列 AcceptedQty 是输入字段
我总共有 5 列 Code,Item Name,unitcode,Quantity 和 AcceptedQty 其中两个 Quantity 和 AcceptedQty 具有相同的值,但 AcceptedQty 是输入字段,因此用户可以在其中输入任何数字,我已将 type="number" 设为仅输入数字
我想要做什么
- 当用户在输入字段中输入任何数字时,不应允许他输入更大的数字到相应的数量
- 假设
code1326Quantity是3,所以在编辑AcceptedQty时我想限制用户不要输入任何大于3 的数字 - 这里我有一个 HTML 表格和这么多行,这就是为什么很难做到
片段
var tableDataDraft = [{
"Code": "1326",
"Item Name": "PINEAPPLE KG",
"unitcode": "NOS",
"Quantity": "3.0000",
"AcceptedQty": "3.0000"
},
{
"Code": "1494",
"Item Name": "2D CAKE CHARGES PER KG",
"unitcode": "NOS",
"Quantity": "3.0000",
"AcceptedQty": "3.0000"
}
]
function addTableDraft(tableDataDraft) {
var col = Object.keys(tableDataDraft[0]);
var countNum = col.filter(i => !isNaN(i)).length;
var num = col.splice(0, countNum);
col = col.concat(num);
var table = document.createElement("table");
var tr = table.insertRow(-1);
for (var i = 0; i < col.length; i++) {
var th = document.createElement("th");
th.innerHTML = col[i];
tr.appendChild(th);
tr.classList.add("text-center");
tr.classList.add("head")
}
for (var i = 0; i < tableDataDraft.length; i++) {
tr = table.insertRow(-1);
for (var j = 0; j < col.length; j++) {
let tabCell = tr.insertCell(-1);
var hiddenField = document.createElement("input"); //creating input field hidden
hiddenField.style.display = "none";
var tabledata = tableDataDraft[i][col[j]];
if (tableDataDraft[i]['Code'] === tableDataDraft[i][col[j]]) { //now setting html attributes
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Item_Code');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (tableDataDraft[i]['Item Name'] === tableDataDraft[i][col[j]]) {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Item_Name');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (tableDataDraft[i]['unitcode'] === tableDataDraft[i][col[j]]) {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Unit_code');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (col[j] === 'Quantity') {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Quantity');
tabCell.appendChild(hiddenField);
}
if (col[j] === 'AcceptedQty') {
var quantityField = document.createElement("input");
quantityField.style.border = "none";
quantityField.style["text-align"] = "right";
quantityField.setAttribute("name", "AcceptedQty");
quantityField.setAttribute("autocomplete", "on");
quantityField.setAttribute("value", tabledata);
quantityField.setAttribute("type", "number");
quantityField.setAttribute("required", "required");
quantityField.classList.add("dataReset");
quantityField.toLocaleString('en-IN');
tabCell.appendChild(quantityField);
}
if (j > 1)
tabCell.classList.add("text-right");
}
}
var divContainer = document.getElementById("table");
divContainer.innerHTML = "";
divContainer.appendChild(table);
table.classList.add("table");
table.classList.add("table-striped");
table.classList.add("table-bordered");
table.classList.add("table-hover");
}
addTableDraft(tableDataDraft)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<div class="table-responsive" id="commonDvScroll">
<table id=table></table>
</div>
【问题讨论】:
-
"我已经让 type="tel" 只输入数字" ?!
type="number"。 "tel" 是一个电话号码。 -
@dheerajkumar 可以使用javascript onchange函数,可以限制AcceptedQty中的数量
-
@CaptainPlanet 那是怎么回事?我真的很难做到这一点
-
@dheerajkumar 这可能会帮助你stackoverflow.com/questions/5704957/…
-
@CaptainPlanet 是的,这很有帮助
标签: javascript jquery html-table