0

JSP があり、Bean の ArrayList にアクセスして、HTML テーブルに製品の詳細を入力します。テーブルには「数量」テキストボックスも表示されます。ユーザーが入力すると、値が JS 関数に転送されます。JS 関数は、製品の合計価格 ("数量" * "単価") を計算する必要があります。「数量」テキストボックスにアクセスしてその値 onChange を取得しようとしていますが、null または undefined しか取得できません。オンラインで入手できる多くのソリューション (特に SO) を試しましたが、うまくいきません。どんな助けでも大歓迎です。

私のコードは次のようになります。

JS コード

<script type = "text/javascript">
    function calculateTotalPrice(tbval) {

 var t = tblContents.getElementsByTagName('qtyText')[0];
     alert("total qty is :" +t);
 var x = tblContents.getElementsByTagName('price')[0];
     alert("price is :" +x);
 var y = t*x;
 var h = tblContents.getElementsByTagName('totalPrice')[0];
 h.value = y;
alert("price set")
</script>

JSP コード

<table>
 <c:forEach items="${beanListInServlet1}" var= "exBean">
    <form action="ExampleServlet" method = "post" name = "tableForm">
       <input type="hidden" name="Id" value= "<c:out Value = "${exBean.Id}"/>"  />

<tr>         
      <td>
        <c:out Value = "${exBean.price}"/>            
      </td>

      <td>
        <input name = "qtyText" type = "textbox" size = "2"  defaultValue = "0" onChange         
          = "calculateTotalPrice()"/>
      </td>

      <td>
         <input name ="price" type = "hidden" value= "<c:out Value = "${exBean.price}"/>"  />
         <input name = "totalprice" type = "textbox" border = "0" size = "2" defaultValue    
           = "0" />          
      </td>
   </c:forEach>  
  </form>
</table>
4

1 に答える 1

0

これを関数呼び出しに渡す必要があります。

 calculateTotalPrice(this)


 here is some addition info:



  function calculateTotalPrice(tbval) {

 myForm = document.forms[0];
 txtEntry = tbval.value;// or myForm.elements["name"].value
        alert("total qty is :" + txtEntry);
    var x = myForm.elements["price"].value
        alert("price is :" +x);
    var y = txtEntry*x;
    var h = tblContents.getElementsByTagName('totalPrice')[0];
  //  h.value = y;
  //  alert("price set")
于 2013-03-07T02:32:02.703 に答える