37

编辑: 这是一个 JSfiddle

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 中使用的语法似乎有问题 - 但我不确定。

4

4 回答 4

55

请仅更改函数的名称;无需其他更改

<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>
于 2013-03-25T17:38:50.287 回答
20

由于这种行为有点奇怪,我对这种行为进行了一些测试,这是我的结果:

TL;博士

如果你是:

  • 在一个form, 和
  • onclick="xxx()"在元素上使用
  • 不要添加id="xxx"或添加name="xxx"到该元素
    • (例如<form><button id="totalbandwidth" onclick="totalbandwidth()">BAD</button></form>

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

对照样例(可以成功调用函数)

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 id="totalbandwidth" onclick="totalbandwidth()">FAILED</button>
</form>

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

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>

于 2019-09-09T04:21:27.967 回答
4

我能够通过遵循此线程中的答案来弄清楚:https ://stackoverflow.com/a/8968495/1543447

基本上,我将所有值、函数名称和元素名称重命名为不同的值,这样它们就不会发生冲突——它奏效了!

于 2013-03-25T17:36:24.250 回答
0

如果您的函数中有一个大写字母,则会发生这种问题,因此请将其更改为小写字母。它会起作用,否则会使函数的名称变短

于 2021-05-28T05:54:28.843 回答