Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.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/8/lua/3.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
在一个html页面中使用javascript计算2组输入类型_Javascript_Html_Function - Fatal编程技术网

在一个html页面中使用javascript计算2组输入类型

在一个html页面中使用javascript计算2组输入类型,javascript,html,function,Javascript,Html,Function,我有两套输入类型的文本框。每个字段上有2个字段。我试图在一页中分别计算和比较每一组数据。 输入类型具有不同的ID <input type="text" id="tmcp_textfield_1" name="blueberry" placeholder="blueberry" value="0" onkeydown="calculate()" onkeyup="calculate()"> <input type="text" id="tmcp_textfield_2" na

我有两套输入类型的文本框。每个字段上有2个字段。我试图在一页中分别计算和比较每一组数据。 输入类型具有不同的ID

<input type="text" id="tmcp_textfield_1" name="blueberry" 
placeholder="blueberry" value="0" onkeydown="calculate()" 
onkeyup="calculate()">
<input type="text" id="tmcp_textfield_2" name="plums" placeholder="plums" value="0" 
onkeydown="calculate()" onkeyup="calculate()">
<input type="text" id="a3" name="a3" placeholder="a3" value="0">

<br>second set below<br>
<input type="text" id="tmcp_textfield_3" name="blueberry" 
placeholder="blueberry" value="0" onkeydown="calculate()" 
onkeyup="calculate()">
<input type="text" id="tmcp_textfield_4" name="plums" placeholder="plums" value="0" 
onkeydown="calculate()" onkeyup="calculate()">
<input type="text" id="a3`" name="a3" placeholder="a3" value="0">


下面第二组
标题中的我的javascript: 第一套:

<script type="text/javascript">
    calculate = function() {
        var blueb = parseFloat($('#tmcp_textfield_1').val());//document.getElementById('blueberry').value;
        var plumsb = parseFloat($('#tmcp_textfield_2').val());//document.getElementById('plums').value; 
        var thetotal = /*document.getElementById('a3').value =*/ parseInt(blueb)+parseInt(plumsb);
        if (thetotal > 6) {
            $('#tmcp_textfield_2').val('');
            $('#tmcp_textfield_1').val('');
            alert('Combination must be below 6');
        }
    }
</script>

计算=函数(){
var blueb=parseFloat($('#tmcp_textfield_1').val());//document.getElementById('blueberry').value;
var plumsb=parseFloat($('#tmcp_textfield_2').val());//document.getElementById('plums').value;
var thetotal=/*document.getElementById('a3')。value=*/parseInt(blub)+parseInt(plumsb);
如果(总数>6){
$('#tmcp_textfield_2').val('');
$('#tmcp_textfield_1').val('');
警报(“组合必须低于6”);
}
}
第二组:

<script type="text/javascript">
    calculate = function() {
        var blueb = parseFloat($('#tmcp_textfield_3').val());//document.getElementById('blueberry').value;
        var plumsb = parseFloat($('#tmcp_textfield_4').val());//document.getElementById('plums').value; 
        var thetotal = /*document.getElementById('a3').value =*/ parseInt(blueb)+parseInt(plumsb);

        if (thetotal > 12){
            $('#tmcp_textfield_4').val('');
            $('#tmcp_textfield_3').val('');
            alert('Combination must be below 12');
        }
    }
</script>

计算=函数(){
var blueb=parseFloat($('#tmcp_textfield_3').val());//document.getElementById('blueberry').value;
var plumsb=parseFloat($('#tmcp_textfield_4').val();//document.getElementById('plums').value;
var thetotal=/*document.getElementById('a3')。value=*/parseInt(blub)+parseInt(plumsb);
如果(总数>12){
$('#tmcp_textfield_4').val('');
$('#tmcp_textfield_3').val('');
警报(“组合必须低于12”);
}
}
这个问题只是第一套计算工作。当我删除第一组javascript时,第二组只起作用,反之亦然。 如何在javascript中区分这些集合,使两个输入集合在一个html页面中协同工作。

问题: 两个集合的函数“calculate()”名称相同,这就是为什么一次只有1个在工作

解决方案: 将函数名重命名为不同的名称,如
calculateOne()
calculatewo()
,则两者都可以使用

希望这有助于解决问题: 两个集合的函数“calculate()”名称相同,这就是为什么一次只有1个在工作

解决方案: 将函数名重命名为不同的名称,如
calculateOne()
calculatewo()
,则两者都可以使用


希望这有帮助

您不能有两个同名的不同函数。将名称更改为calculate1和calculate2或其他一些有意义的名称。此外,使用
var
,,
const
前面计算
,这样你就不会将它绑定到窗口对象。你能区分它们哪些函数在相同的范围内具有相同的名称吗?是的,这很酷…但是我打算进一步使用php使输入字段动态化,这样我就不会有两个不同的函数名称了在1输入字段下,不能有两个同名的不同函数。将名称更改为calculate1和calculate2或其他一些有意义的名称。此外,使用
var
,,
const
前面计算
,这样你就不会将它绑定到窗口对象。你能区分它们哪些函数在相同的范围内具有相同的名称吗?是的,这很酷…但是我打算进一步使用php使输入字段动态化,这样我就不会有两个不同的函数名称了在1个输入字段下是的,效果很好…但我打算使用php进一步使输入字段动态化,这样我在1个输入字段下可能没有2个不同的函数名。您可以在函数中发送参数,以计算所需字段的值。是的,效果很好…但我打算使用php使输入字段动态化php进一步这样做,在1个输入字段下可能没有2个不同的函数名。您可以在函数中发送参数,以计算所需字段的值。