简体   繁体   English

函数中的innerHTML(全局变量?)

[英]innerHTML in function (Global variables?)

I'm struggling to get this working. 我很难让这个工作。 The dispatch() function seems to be getting triggered (tested with the alert), but the innerHTML lines don't seem to work. dispatch()函数似乎被触发(使用警报测试),但innerHTML行似乎不起作用。

Also, i doesn't seem to increase despite the i++ in the onSubmit . 此外,尽管onSubmiti++i似乎并没有增加。

Here is the function in question: 这是有问题的功能:

function dispatch(passengers,i,timesArray)
{
    alert('value of i is '+i);
    timesArray[i]=getTime();

    var avTime=getAverageTime(timesArrary);

    var throughput=passengers*3600000/avTime;

    if(i==0)
    {
        document.getElementById('output').innerHTML = 'Calculating...';
    }
    else
    {
        document.getElementById('output').innerHTML = throughput;
    }
    //and possibly a list (w/e)
}

And here is the form: 以下是表格:

<form method="post" action="javascript:void(0);" name="applesForm" onSubmit="dispatch(document.applesForm.numPassengers.value, num, times);i++;">
    <input type="text" name="numApples" id="numPassengers" />
    <br/>
    <input type="submit" name="Submit" value="Press on Dispatch!"/>
</form>

Could this be a question of not being able to change global variables from inside the function? 这可能是一个无法从函数内部更改全局变量的问题吗?

Or, is there something wrong with the avTime or throughput lines which is making the function cease? 或者, avTimethroughput线是否有问题导致功能停止?

Thanks. 谢谢。

In this line: 在这一行:

<form method="post" action="javascript:void(0);" name="applesForm" onSubmit="dispatch(document.applesForm.numPassengers.value, num, times);i++;">

i is a global variable, but in dispatch() i is an argument which is not in global scope. i是一个全局变量,但在dispatch() i是一个不在全局范围内的参数。 Inside dispatch() it is in local scope of that function, and can't be increased in global scope. dispatch()内部,它位于该函数的局部范围内,并且不能在全局范围内增加。 Hence I think your onSubmit() should be: 因此我认为你的onSubmit()应该是:

onSubmit="dispatch(document.applesForm.numPassengers.value, num, times);num++;">

Why is the variable being increased outside the function? 为什么变量在函数外增加? Shouldn't it be inside the function and increased there? 它不应该在功能内并增加吗?

Also, if num is global, you don't need to pass it as a parameter. 此外,如果num是全局的,则不需要将其作为参数传递。

// global
var num = 0;

function dispatch(passengers, timesArray)
{
    alert('value of num is ' + num);
    timesArray[num]=getTime();

    var avTime = getAverageTime(timesArrary);

    var throughput = passengers * 3600000 / avTime;

    if(num == 0)
    {
         document.getElementById('output').innerHTML = 'Calculating...';
    }
    else
    {
        document.getElementById('output').innerHTML = throughput;
    }
    //and possibly a list (w/e)

    // increasing num here!
    num++;
}

Then your form could drop the increase in the variable, and the num from the call. 然后你的表单可以放弃变量的增加,以及调用中的num。

<form method="post" action="javascript:void(0);" name="applesForm" onSubmit="dispatch(document.applesForm.numPassengers.value, times)">

Cheers, Apoc. 干杯,Apoc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM