[英]How to find the number of positive and negative numbers are there on an input field?
我的问题是有 3 个输入文本字段和一个按钮,无论用户输入并按下按钮,警报都应该说明文本字段上有多少负数和多少正数?
Here is a working example The code is explained as below.这是一个工作示例代码解释如下。
If you want to enter a number in each of the text field and click the button which tells the number of positive and negative numbered entered then here is your code,如果您想在每个文本字段中输入一个数字,然后单击告诉输入的正负编号数量的按钮,那么这里是您的代码,
You HTML code,你的 HTML 代码,
<input id="one" type="text"/>
<input id="two" type="text"/>
<input id="three" type="text"/>
<input type="button" onclick="check();" value="check">
Now you can write the script as follows,现在您可以按如下方式编写脚本,
function check(){
//Converting the values to integer
var x = parseInt(document.getElementById("one").value);
var y = parseInt(document.getElementById("two").value);
var z = parseInt(document.getElementById("three").value);
//Initializing the positive and negative count to 0
var posCount = 0;
var negCount = 0;
//Incrementing postive and negative count depending whether it is positive or negative
if(x>=0){
posCount++;
}else{
negCount++;
}
if(y>=0){
posCount++;
}else{
negCount++;
}
if(z>=0){
posCount++;
}else{
negCount++;
}
alert("Positive numbers: "+posCount);
alert("Negative numbers: "+negCount);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.