我已经定义了函数的javascripts中的错误是什么,但它无法找到定义

时间:2018-01-12 12:45:03

标签: javascript

hat is the error in i had already define the function but it could not find the defination please help

<video id="video" src=" video path from my local directory " controls autoplay></video>
< script language = "JavaScript"
type = "text/javascript"
src = "JavaScript.js" >
  var inp = document.getElementById('inp');
var conv = document.querySelectorAll('conv');

function convWt() {
  var val = inp.value;
  conv[0].value = parseFloat(val * 1000);
  conv[1].value = parseFloat(val * 2.2046);
  conv[2].value = parseFloat(val * 1000000);
  conv[3].value = parseFloat(val * 0.0011023);
  conv[4].value = parseFloat(val * 35.274);
} <
/script>

上述代码中的错误是什么? 错误是未被引用的引用

帽子是我已经定义了该功能的错误,但它无法找到防守请帮助

3 个答案:

答案 0 :(得分:2)

你的onClick是指由于拼写错误而导致的一个不存在的功能,请查看ref&#39; convWT&#39; vs func def&#39; convWt&#39;

答案 1 :(得分:2)

您正在尝试在已加载javascript文件的脚本标记内执行javascript。这是不可能的。尝试加载js文件和另一个脚本标记,使用您的代码。像这样:

<script language="JavaScript" type="text/javascript" src="JavaScript.js"></script>
<script>
  var inp = document.getElementById('inp');
var conv = document.querySelectorAll('conv');

function convWt() {
  var val = inp.value;
  conv[0].value = parseFloat(val * 1000);
  conv[1].value = parseFloat(val * 2.2046);
  conv[2].value = parseFloat(val * 1000000);
  conv[3].value = parseFloat(val * 0.0011023);
  conv[4].value = parseFloat(val * 35.274);
} 
</script>

同样在您的HTML部分中,您调用 convWT(),但函数名称为 convWt(),没有大写字母T.

希望有所帮助。

编辑:变量 inp conv 可能为null / undefined,因为在执行js代码时未加载DOM。最简单的&#34;方法是使用jQuerys ready 功能。它将等待呈现DOM并执行您的代码。仅供参考:https://learn.jquery.com/using-jquery-core/document-ready/

答案 2 :(得分:0)

Firstable正如我所说convWt()convWT()不同 然后,您不应该为每个输入提供相同的id
试着这样做:

var inp = document.getElementById('inp');
var convGram = document.getElementById('convGram');
var convPound = document.getElementById('convPound');
var convMiliGram = document.getElementById('convMiliGram');
var convTon = document.getElementById('convTon');
var convOunce = document.getElementById('convOunce');


function convWt() {
  var val = inp.value;
  
  convGram.value = parseFloat(val * 1000);
  convPound.value = parseFloat(val * 2.2046);
  convMiliGram.value = parseFloat(val * 1000000);
  convTon.value = parseFloat(val * 0.0011023);
  convOunce.value = parseFloat(val * 35.274);
}
<center>
  <h1>Weight Converter</h1>
</center>
<center>
  <p>Welcome To Online Weight Converter</p>
</center>

<center><label>Weight In Kg:</label>
  <form id="myForm">

    <input id="inp" type="number" />
    <input type="button" value="convert" onclick="convWt()" />

    <table>

      <tr>
        <td>Weight In Grams:</td>
        <td><input id="convGram" /></td>
      </tr>
      <tr>
        <td>Weight In Pound</td>
        <td><input id="convPound" /></td>
      </tr>
      <tr>
        <td>MiliGram:</td>
        <td><input type="text" id="convMiliGram" /></td>
      </tr>
      <tr>
        <td>Us Ton:</td>
        <td><input type="text" id="convTon" /></td>
      </tr>
      <tr>
        <td>Ounces:</td>
        <td><input id="convOunce"></td>
      </tr>
      <tr>
        <td><input type="button" value="Rest Form" onclick="document.getElementById('myForm').reset()" /></td>
      </tr>
    </table>
  </form>
  <p><b/> You Can Press Reset Button For Another Conversion
  </p>
</center>

写作时间更长,是的,但它充其量只是。