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

时间:2019-04-02 09:59:50

标签: javascript jquery html-table

我有一个HTML表,其中有一个列AcceptedQty,它是输入字段

我总共有5列CodeItem NameunitcodeQuantityAcceptedQty,其中两列分别是Quantity和{{1} }具有相同的值,但是AcceptedQty是输入字段,因此用户可以在其中输入任何数字,而我已将type =“ number”设置为仅输入数字

我要做什么

  • 当用户在输入字段中输入任何数字时,不应允许他输入对应数量的更大数字
  • 假设AcceptedQty 1326 code为3,所以在编辑Quantity时我想限制用户输入的数字不得大于3
  • 这里我有一个HTML表和很多行,这就是为什么很难做到这一点

代码段

AcceptedQty
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)

2 个答案:

答案 0 :(得分:1)

  

并且我已经键入“ =“ tel”以仅输入数字

使用type="number""tel"是电话号码)以及{{的minmax属性(以及反映的属性) 3}}(如果您不希望使用小数,则使用step)。可能还包括一个input处理程序,以处理没有HTML5字段功能的浏览器。

请参见***注释行:

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>

请注意,用户仍然可以输入一个更大的数字,但是该表格无法验证。有关MDN,请参见HTMLInputElement

答案 1 :(得分:0)

如@ T.J Crowder所述,请使用输入类型number

该输入类型允许您设置一个max值,该值基于先前的列值

此外,您还想向输入添加事件侦听器以“监听” /检测输入的更改,以便您可以对任何更改进行操作,并根据需要限制输入值


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>