未捕获的TypeError:无法设置undefined的属性'value' - JS

时间:2017-02-12 17:05:32

标签: javascript jquery

enter image description here

大家好,有人可以帮我解决这个错误吗?我想弄清楚它3个小时但仍然无法解决。它在事件的第一次尝试或第一次火灾中工作,但如果事件再次发生,则它不再起作用。

这是脚本。

<script type="text/javascript" language="Javascript">
  var sum=0;
    function OnChange(value){

    price =   document.getElementById('select1').value;      
    quantity = document.getElementById('select2').value;  
        sum = price * quantity;

    document.frmOne.txtDisplay.value = sum;
    }
</script>
<SCRIPT language=Javascript>
        <!--
        function isNumberKey(evt)
        {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
          return false;

         return true;
        }
        //-->
    </SCRIPT>

这是表格

<form NAME = "frmOne" action="initiateorder.php" method="post" enctype="multipart/form-data">
  <input type="hidden" name="transnum" value="<?php echo $_GET['trnasnum'] ?>" />
  <INPUT TYPE = "Text" name = "select1" size = "35" id="select1" value ="<?php echo $price ?>" style="display:none;">
  <INPUT TYPE = "Text" name = "pname" size = "35" value ="<?php echo $name ?>" style="display:none;">
    <br>
    <span style="font-size:11px; font-family:Arial, Helvetica, sans-serif; text-align:left; line-height:17px;color:#000000;">Quantity : </span>
  <input type="text" name="select2" id="select2" onkeyup="OnChange(this.value)" onKeyPress="return isNumberKey(event)" style="width:60px;" /> 

   <span style="color:#B80000; font-size:16px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;">=</span> 
    <INPUT TYPE = "Text" name = "txtDisplay" size = "35" value ="<?php echo $price ?>" style="border:#999999 solid 1px; background-color:#FFF; width:100px; height:20px;" readonly>
</form>

1 个答案:

答案 0 :(得分:1)

尝试使用document.getElementsByName("txtDisplay")[0].value = sum代替document.frmOne.txtDisplay.value = sum;