如何解决Uncaught TypeError:无法将undefined或null转换为对象

时间:2018-06-30 10:29:32

标签: javascript jquery html

这是我的html。我想计算以英尺,厘米,英寸为单位的高度。当我输入高度值时,我想将其转换为英寸和英尺,反之亦然。请帮我。

-g

这是我的JavaScript函数

<form name=cminch method="post">
    <div>
        <label>feet</label>
        <input onkeyup="feetconvert()" name="Heightft">
        <label>inch</label>
        <input onkeyup="inchconvert()"  name="HeightIn">
        <label>Cm</label>
        <input onkeyup="cmconvert()"" name="Heightcm"> 
    </div>
</form>

1 个答案:

答案 0 :(得分:0)

只需将整个代码复制并粘贴到文件中,然后尝试运行相同的代码,并让我知道您是否仍然遇到相同的错误。

将示例检入JSFiddle

<form name="cminch" method="post">
    <div>
      <label>feet</label>
      <input onkeyup="feetconvert()" name="Heightft">
      <label>inch</label>
      <input onkeyup="inchconvert()"  name="HeightIn">
      <label>Cm</label>
      <input onkeyup="cmconvert()" name="Heightcm"> 
    </div>
</form>

<script>
    function roundit(which){
        return Math.round(which*100)/100
    }

    function cmconvert(){
        with (document.cminch){
            Heightft.value = roundit(Heightcm.value/30.84);
            HeightIn.value = roundit(Heightcm.value/2.54);
        }
    }
    function inchconvert(){
        with (document.cminch){
            Heightcm.value = roundit(HeightIn.value*2.54);
            Heightft.value=roundit(HeightIn.value/12);
        }
    }
    function feetconvert(){
        with (document.cminch){
            Heightcm.value=roundit(Heightft.value*30.48);
            HeightIn.value=roundit(Heightft.value*12);
        }
    }
</script>