【问题标题】:How to restrict input field value to certain number如何将输入字段值限制为某个数字
【发布时间】:2019-04-02 20:35:36
【问题描述】:

我有一个 HTML 表格,里面有一列 AcceptedQty 是输入字段

我总共有 5 列 Code,Item Name,unitcode,QuantityAcceptedQty 其中两个 QuantityAcceptedQty 具有相同的值,但 AcceptedQty 是输入字段,因此用户可以在其中输入任何数字,我已将 type="number" 设为仅输入数字

我想要做什么

  • 当用户在输入字段中输入任何数字时,不应允许他输入更大的数字到相应的数量
  • 假设code 1326 Quantity 是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


【解决方案1】:

我已经让 type="tel" 只输入数字

使用type="number""tel" 是一个电话号码)和HTMLInputElement(和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 上的 this tutorial

【讨论】:

  • 我试过这种东西,通过键盘看我可以输入大于 3
  • @dheerajkumar - 没错,但表格无效。我已经添加了一个指向 HTML5 表单验证的 MDN 教程的链接。 (一般来说,阻止用户输入很容易出错并且不是很好的用户体验;相反,告诉他们输入的内容不正确,以便他们修复它。)
  • 但我没有使用 form.submit 提交表单我只是使用 .click 进行 ajax 调用,所以在这种情况下表单验证将不起作用
  • @dheerajkumar - 阅读文章。 :-) 您可以在不提交表单的情况下使用验证功能。
  • @dheerajkumar - 例如,我更新了上面的 sn-p 以使用 :invalid 伪类在字段无效时对其进行标记。
【解决方案2】:

正如@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>

【讨论】:

  • 嘿,我可以输入大于 3 的数字,请检查您的 sn-p
  • 再次花费大于 3
  • 嘿,我将 this.value = val; 替换为 alert("somealert") 但问题是它没有关注具有更大价值的输入字段
  • 使用警报和一些 css 样式进行更新
  • 嘿,为什么不使用 2.5?先生,你在吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
  • 2014-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多