0

我有一个 JSP,并且访问了一个 bean 的 ArrayList 来填充一个包含产品详细信息的 HTML 表。该表还显示了一个“数量”文本框,当用户填写该文本框时,应将值传递给 JS 函数。JS 函数反过来应该计算产品的总价格(“数量”*“单价”)。我正在尝试访问“数量”文本框并获取其值 onChange,但我只得到空值或未定义。我已经尝试了许多在线可用的解决方案(尤其是 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

你需要把它传递你的函数调用,ala

 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 回答