每个元素的输入数字对象

时间:2019-10-21 07:37:21

标签: javascript html

如何为每个元素添加一个输入数字对象。每个产品应该有一个输入编号对象,以便我们可以决定需要多少个产品。

例如:我需要购买一种产品,我要其中5种。因此,我只是在“数字”框中滚动,然后选择需要的产品数量。

JavaScript

function myFunction() {
  var x = document.getElementById("myNumber").value;
  document.getElementById("demo").innerHTML = x;
}

HTML

<input type="number" id="myNumber" value="1">

2 个答案:

答案 0 :(得分:1)

也许您要这个:

通过 Jquery

$(function(){
      $('#demo').text($('#sel').val());
      $('#sel').change(function(){
         $('#demo').text($(this).val());
      });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="sel">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
</select>

<div id="demo" style="font-weight: bold"><div>

通过 JavaScript

myF();
function myF(){
  var a = document.getElementById("sel").value;
  document.getElementById("demo").innerHTML = a;
}
    <select id="sel" onchange="myF();">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
      <option>6</option>
      <option>7</option>
      <option>8</option>
    </select>

    <div id="demo" style="font-weight: bold"><div>

答案 1 :(得分:0)

function myFunction() {
  var x1 = document.getElementById("itm1").value;
  var x2 = document.getElementById("itm2").value;
  var x3 = document.getElementById("itm3").value;
  var x4 = document.getElementById("itm4").value;
  var x5 = document.getElementById("itm5").value;

alert("Item1: "+ x1 +"\nItem2: " +x2+ "\nItem3: " +x3+ "\nItem4: " +x4+ "\nItem5: " +x5)
}
<label>Item 1 </label><input type="number" id="itm1" value="1"><br/>
<label>Item 2 </label><input type="number" id="itm2" value="1"><br/>
<label>Item 3 </label><input type="number" id="itm3" value="1"><br/>
<label>Item 4 </label><input type="number" id="itm4" value="1"><br/>
<label>Item 5 </label><input type="number" id="itm5" value="1"><br/>
<input type="button" value="Submit" onclick="myFunction()"/>