在if else语句javascript中使用输入字段

时间:2016-04-15 12:21:57

标签: javascript if-statement

所以我试图将一个元素提交到一个输入字段并使用if else语句返回一个结果,但它不断返回我的" else"声明无论如何。我开始使用w3schools项目,但我似乎无法看到出了什么问题。

用户将放一个数字是" numSpots"输入字段,根据数字的值,adaSpots值将在段落id =" demo"中返回。

这是我的代码



Pitch: E5
Pitch: G5
Pitch: A5
Pitch: D5
Pitch: F#5
Pitch: A5
Pitch: D5
Pitch: F#5
Pitch: A5
Pitch: C6
Pitch: G4
Pitch: B4
Pitch: D5
*************

function myFunction() {
  var numSpots = document.getElementById("numSpots");
  var adaSpots;
  if (numSpots < 25) {
    adaSpots = "1";
  } else {
    adaSpots = "267";
  }
  document.getElementById("demo").innerHTML = adaSpots;
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您需要#numbSpots

获取document.getElementById("numSpots").value的值

&#13;
&#13;
function myFunction() {
  var numSpots = document.getElementById("numSpots").value;
  var adaSpots;
  if (numSpots < 25) {
    adaSpots = "1";
  } else {
    adaSpots = "267";
  }
  document.getElementById("demo").innerHTML = adaSpots;
}
&#13;
<p>Click the button to display a time-based greeting:</p>
<input type="number" name="numSpots" id="numSpots" />
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个;)

您需要使用<script> $(".addRow").click(function (){ //propogate a row before the current last row //I have the following but unsure of how to add before the last row /*$("#formId").append("<input type="text" name="type" class="colData"> <input type="text" name="rowNum_#" class="colData"> <input type="text" name="rowX_#" class="colData"> <input type="text" name="rowY_#" class="colData"> <input type="text" name="rowZ_#" class="colData"> <input type="text" name="type_#" class="colData">");*/ }); </script> 函数将字符串转换为整数值,因为文本字段值以字符串形式返回;并且您忘记使用parseInt属性获取值;

value