javascript JS:未捕获的类型错误:对象不是函数(onclick)

c9x0cxw0  于 2023-03-06  发布在  Java
关注(0)|答案(4)|浏览(108)
    • Edit2:**错误在此行:<input type="button" value="totalbandwidthresult" onclick="javascript:totalbandwidth();">

尝试让一个按钮执行计算。所需的变量如下,以及HTML,其中
我在单击时收到错误:Uncaught TypeError: object is not a function index.html:71 onclick
这是我的Javascript

function totalbandwidth() {  
    var fps=Number(document.calculator.fps.value);  
    var bitrate=Number(document.calculator.bitrate.value);  
    var numberofcameras = Number(document.calculator.numberofcameras.value); 
    var encoding = document.calculator.encoding.value; 
    if (encoding = "mjpeg")
    {
        storage = bitrate*fps;
    }
    else
    {
        storage = bitrate;
    }

    totalbandwidth = (numberofcameras * storage) / 1000;
    document.calculator.totalbandwidthresult.value = totalbandwidth;  
}

HTML:

<form name="calculator" class="formtable">  
<div class="formrow"><label for="rcname">RC Name</label> <input type="text" name="rcname"></div>  
<div class="formrow"><label for="fps">FPS</label> <input type="text" name="fps">  </div>  
<div class="formrow"><label for="bitrate">Bitrate</label> <input type="text" name="bitrate">  </div>  
<div class="formrow"><label for="numberofcameras">Number of Cameras</label> <input type="text" name="numberofcameras"> </div>   
<div class="formrow"><label for="encoding">Encoding</label> <select name="encoding" id="encodingoptions">
  <option value="h264">H.264</option>
  <option value="mjpeg">MJPEG</option>
  <option value="mpeg4">MPEG4</option>
</select></div>  
Total Storage: <input type="text" name="totalstorage">   
Total Bandwidth: <input type="text" name="totalbandwidth">   
<input type="button" value="totalbandwidthresult" onclick="javascript:totalbandwidth();">

基本上--看起来我在JS中使用的语法可能有问题--但我不确定。

gcuhipw9

gcuhipw91#

请只更改函数的名称;不需要其他更改

<script>
    function totalbandwidthresult() {
        alert("fdf");
        var fps = Number(document.calculator.fps.value);
        var bitrate = Number(document.calculator.bitrate.value);
        var numberofcameras = Number(document.calculator.numberofcameras.value);
        var encoding = document.calculator.encoding.value;
        if (encoding = "mjpeg") {
            storage = bitrate * fps;
        } else {
            storage = bitrate;
        }

        totalbandwidth = (numberofcameras * storage) / 1000;
        alert(totalbandwidth);
        document.calculator.totalbandwidthresult.value = totalbandwidth;
    }
</script>

<form name="calculator" class="formtable">
    <div class="formrow">
        <label for="rcname">RC Name</label>
        <input type="text" name="rcname">
    </div>
    <div class="formrow">
        <label for="fps">FPS</label>
        <input type="text" name="fps">
    </div>
    <div class="formrow">
        <label for="bitrate">Bitrate</label>
        <input type="text" name="bitrate">
    </div>
    <div class="formrow">
        <label for="numberofcameras">Number of Cameras</label>
        <input type="text" name="numberofcameras">
    </div>
    <div class="formrow">
        <label for="encoding">Encoding</label>
        <select name="encoding" id="encodingoptions">
            <option value="h264">H.264</option>
            <option value="mjpeg">MJPEG</option>
            <option value="mpeg4">MPEG4</option>
        </select>
    </div>Total Storage:
    <input type="text" name="totalstorage">Total Bandwidth:
    <input type="text" name="totalbandwidth">
    <input type="button" value="totalbandwidthresult" onclick="totalbandwidthresult();">
</form>
kt06eoxx

kt06eoxx2#

由于行为有点奇怪,我对行为做了一些测试,下面是我的结果:

靶区; DR

如果您是:

      • form中**,以及
  • 在元素上使用onclick="xxx()"
      • 不要**将id="xxx"name="xxx"添加到该元素
  • (例如不良)BAD**)

以下是一些测试及其结果:

质控样品 (可成功调用函数)

function totalbandwidth(){ alert("Total Bandwidth > 9000Mbps"); }
<form onsubmit="return false;">
  <button onclick="totalbandwidth()">SUCCESS</button>
</form>

将ID添加到按钮 (调用函数失败)

一个二个一个一个

为按钮添加名称 (调用函数失败)

function totalbandwidth(){ alert("Total Bandwidth > 9000Mbps"); }
<form onsubmit="return false;">
  <button name="totalbandwidth" onclick="totalbandwidth()">FAILED</button>
</form>

按钮加值 (可成功调用函数)

function totalbandwidth(){ alert("Total Bandwidth > 9000Mbps"); }
<form onsubmit="return false;">
  <input type="button" value="totalbandwidth" onclick="totalbandwidth()" />SUCCESS
</form>

将ID添加到按钮,但不在表单中 (可以成功调用函数)

function totalbandwidth(){ alert("Total Bandwidth > 9000Mbps"); }
<button id="totalbandwidth" onclick="totalbandwidth()">SUCCESS</button>

将id添加到表单内的另一个元素 (可以成功调用函数)

function totalbandwidth(){ alert("The answer is no, the span will not affect button"); }
<form onsubmit="return false;">
<span name="totalbandwidth" >Will this span affect button? </span>
<button onclick="totalbandwidth()">SUCCESS</button>
</form>
htrmnn0y

htrmnn0y3#

我能够按照这个线程中的答案弄清楚:https://stackoverflow.com/a/8968495/1543447
基本上,我将所有的值、函数名和元素名重命名为不同的值,这样它们就不会冲突--而且成功了!

af7jpaap

af7jpaap4#

如果你的函数中有一个大写字母,那么这种问题就会发生,所以把它改为小写字母。这样会有效,否则会使函数的名称变短

相关问题