Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 比较两个输入字段_Javascript_Html - Fatal编程技术网

Javascript 比较两个输入字段

Javascript 比较两个输入字段,javascript,html,Javascript,Html,我用这个函数比较两个输入字段。如果用户在两个文本字段中输入相同的数字。提交时将出现错误。现在我想知道是否有一种方法允许相同的数字,但不高于或低于前一个文本框的值1。例如,如果用户在上一个文本框中输入5,则用户只能在其他输入字段中输入4、5或6。请给我一些建议 <script type="text/javascript"> function Validate(objForm) { var arrNames=new Array("text1", "text2"); var arrV

我用这个函数比较两个输入字段。如果用户在两个文本字段中输入相同的数字。提交时将出现错误。现在我想知道是否有一种方法允许相同的数字,但不高于或低于前一个文本框的值1。例如,如果用户在上一个文本框中输入5,则用户只能在其他输入字段中输入4、5或6。请给我一些建议

   <script type="text/javascript">
function Validate(objForm) {
var arrNames=new Array("text1", "text2");
var arrValues=new Array();
for (var i=0; i<arrNames.length; i++) {
var curValue = objForm.elements[arrNames[i]].value;
if (arrValues[curValue + 2]) {
alert("can't have duplicate!");
return false;
}
arrValues[curValue] = arrNames[i];
}
return true;
}
</script>

函数验证(objForm){
var arrNames=新数组(“text1”、“text2”);
var arrValues=新数组();
对于(var i=0;i给他们两个ID。
然后使用

if(document.getElementById("first").value == document.getElementById("second").value){
    //they are the same, do stuff for the same
}else if(document.getElementById("first").value >= document.getElementById("second").value
    //first is more than second
}

等等。

这里有一个建议/提示

if (Math.abs(v1 - v2) <= 1) {
    alert("can't have duplicate!");
    return false;
}

if(Math.abs(v1-v2)一种简洁易读的方法:

var firstInput = document.getElementById("first").value;
var secondInput = document.getElementById("second").value;

if (firstInput === secondInput) {
    // do something here if inputs are same
} else if (firstInput > secondInput) {
    // do something if the first input is greater than the second
} else {
    // do something if the first input is less than the second
}

这允许您在比较后再次将值用作变量(firstInput),(secondInput).

你会尝试什么?另外,我不认为你的currenct函数真的检查重复值。如果秒大于秒,你应该做些什么?我想你的意思是如果firstInputfirstInput>secondInput
,但它只验证第一个数字,如何修复它?例:59>9这里它只验证第二个数字ecking 5>9@Ipm我需要检查firstInput>secondInput,但它只验证第一位数字,如何修复它?您可以在验证函数中执行类似操作:if((''+a)[0]@I pm感谢您的快速响应..我没有让您看到我的代码,我想验证
firstInput>secondInput
我使用了这个
document.getElementById('noOfWorkingComputers').value>document.getElementById('noOfComputers')).value
但只对第一个数字进行验证如何修复?例如:59>9这里它只检查5>9这里它只对一个数字工作良好。如果它像字符串一样检查,即使是那个一个数字也不会工作naa?你能发布一个JSFIDLE(或类似的)链接吗?这样就更容易检查你的问题了。
var firstInput = document.getElementById("first").value;
var secondInput = document.getElementById("second").value;

if (firstInput === secondInput) {
    // do something here if inputs are same
} else if (firstInput > secondInput) {
    // do something if the first input is greater than the second
} else {
    // do something if the first input is less than the second
}