简体   繁体   English

如何将输入字段值限制为一定数量

[英]How to restrict input field value to certain number

I have an HTML table inside which i have a column AcceptedQty which is input field 我有一个HTML表,其中有一个AcceptedQty列,它是输入字段

Total i have 5 columns Code , Item Name , unitcode , Quantity and AcceptedQty two of them Quantity and AcceptedQty have same values but AcceptedQty is input field so user can input any number inside that and i have made that type="number" to enter only numbers 我总共有5列CodeItem NameunitcodeQuantityAcceptedQty他们两个QuantityAcceptedQty有相同的价值观,但AcceptedQty是那么用户可以输入输入字段中的任何数量的内部的,我已经提出,类型=“数字”只输入数字

What i am trying to do 我想做什么

  • when user input any number inside the input field it should not allow him to enter greater number to the corresponding quantity 当用户在输入字段中输入任何数字时,不应允许他输入更大的数字以达到相应的数量
  • suppose for code 1326 Quantity is 3 so while editing AcceptedQty i want to restrict user not to enter any number greater then 3 假设code 1326的Quantity为3,所以在编辑AcceptedQty我要限制用户不要输入大于3的任何数字
  • here i have a HTML table and so many rows that's why finding it dificult to do 在这里,我有一个HTML表和这么多行,这就是为什么很难做到这一点的原因

Snippet 片段

 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> 

and i have made that type="tel" to enter only numbers 而且我已经使type =“ tel”仅输入数字

Use type="number" ( "tel" is a telephone number ) and the min and max attributes (and reflected properties) of the HTMLInputElement (and step if you don't want fractional values). 使用type="number""tel"电话号码 )和HTMLInputElementminmax属性(以及反射的属性)(如果您不希望使用小数,则使用step )。 Probably also include an input handler to handle browsers without HTML5 field features. 可能还包括一个input处理程序,用于处理没有HTML5字段功能的浏览器。

See the *** commented lines: 请参阅***注释行:

 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 tablerow = tableDataDraft[i]; // *** var tabledata = tablerow[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.min = 0; // *** quantityField.max = tablerow.Quantity; // *** 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) 
 input:invalid { color: #d00; border: 1px solid #d00 !important; } 
 <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> 

Note that the user will still be able to enter a higher number, but the form won't validate. 请注意,用户仍将能够输入更大的数字,但是该表格将无法通过验证。 See this tutorial on MDN. 请参阅有关MDN的本教程

As @TJ Crowder mentioned, make use of the input type number 如@TJ Crowder所述,利用输入类型number

That input type allows you to set a max value, which is based of the previous columns value 该输入类型允许您设置一个max ,该max基于先前的列值

Additionally, you'll want to add an event listener to the input to "listen" / detect changes to the input, so that you can act upon any changes, restricting the input value as you see fit 此外,您需要向输入添加事件侦听器,以“侦听” /检测输入的更改,以便您可以对任何更改进行操作,并根据需要限制输入值


 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.setAttribute("max", parseInt(tableDataDraft[i]["Quantity"])); quantityField.setAttribute("min", 0); quantityField.setAttribute("step", 0.1); quantityField.addEventListener("change", function() { let max = parseFloat(this.getAttribute('max')); let min = parseFloat(this.getAttribute('min')); let val = parseFloat(this.value) val = val > max ? max : val; val = val < min ? min : val; if (val != this.value) { alert("Input is incorrect"); this.focus(); } this.value = val; }); quantityField.setAttribute("min", 0); 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) 
 input:invalid { outline: 1px solid red; } 
 <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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何限制输入字段的值? - How to restrict value for Input field? 如何动态限制重复值<input='text' />在一个循环的随机值中<input='number' />场地 - How to dynamically Restrict Duplicated value of <input='text'/> in a looped random value from <input='number'/> field 如何仅在输入字段中限制数值? - How to restrict only numeric value in input field? JavaScript,HTML,CSS-如何使用-有效性检查来限制输入字段中允许的数字/值 - JavaScript, HTML, CSS - How to Restrict Number / Value Allowed in Input Field With - Validity Check 如何限制用户在类型为文本的输入字段中输入数字? - How to restrict user from entering number in to an input field with type as text? 如何在手动输入时限制html5数字输入的最大值 - How to restrict max value on html5 number input on manual entry 如何限制在输入类型号中输入十进制值? - How to restrict from entering a decimal value in input type number? 如何允许用户在小数点前输入一定数量的输入值,在小数点后键入一定数量的输入值 - How to allow user to type in certain number of input value before decimal and certain number of input value after decimal 如何从输入字段中获取数字值? - How to get a number value from an input field? 如何将输入字段的值视为数字? - How to treat input field value as a number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM