0

我有一个invoice.jsp页面,我必须使用 jQuery 或任何其他方式在文本框中计算一些值。

我对 jQuery 了解不多。请帮我解决这个问题。

在我的发票中有一个数量文本框。如果用户输入数量,则计算出的价格应动态计算(total_subPrice= unit_price * quantity),即显示在另一个名为“价格”的文本框中。

再次,所有价格的总和应作为总计出现在按钮中。

请注意:所有行值都来自我的数据库表,基于用户对项目的选择。

我只使用此代码在我的invoice.jsp页面中显示值:

<s:iterator  value="#session.BOK" status="userStatus">
  <tr style="height: 10px;"> 
    <td width="65%" align="left"><s:property value="bookTitile"/></td>
    <td width="10%" align="left"><s:property value="price"/></td>
    <td width="10%" align="center"><s:textfield name="quantity" value="%{quantity}" size="2" /></td>
    <td width="15%" align="center" >
      <s:textfield value="%{price}" name="" size="6"></s:textfield>
    </td>
  </tr>
</s:iterator>

我的invoice.jsp输出如下所示: 在此处输入图像描述

我不知道如何line Total根据选择的数量来计算,并line total在总计文本框中显示所有的总和(见下面的发票图片)。

我也试过 这个,但我仍然无法解决我的问题。

我的完整 JSP 代码:

<table width="100%" height="50%" border="0" cellpadding="0" cellspacing="0">
  <tr>
  <td height="74%">
    <s:form action="dfs" id="form3"  theme="simple">
      <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"  id="your_content">
        <tr>
          <td valign="top" height="10%">
            <div id="invNum">Invoice# 12688</div>
            <div id="ttielMain">Vision Books</div>
            <div id="Orgaddress">    Thamel Kathmandu Nepal</div>
            <div id="phoneNum">  Tel# 00977-1-12173803</div>
            <div id="websiteOrg"> www.thebestbookfinder.com</div>
          </td>
        </tr>
        <tr>
          <td valign="top" width="100%" align="left">
          ----------------------------------------------------------- -----------------------------------
          </td>
        </tr>
        <tr>
          <td height="6%" valign="top" width="100%">
            <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
              <tr style="height: 10px;font-family: serif;font-weight: bold;font-size: 14px;">
                <td width="65%" align="left">Title</td>
                <td width="10%" align="left">Unit Price</td>
                <td width="10%" align="center">Qty</td>
                <td width="15%" align="left">Line Total</td>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td height="1%" valign="top" width="100%">
            <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
              <tr style="height: 10px;">
                <td width="65%" align="left">
                -------------------------------------------------------
                </td>
                <td width="10%" align="left">----------</td>
                <td width="10%" align="center">-----</td>
                <td width="15%" align="left">-------------</td>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td height="65%" valign="top" width="100%">
            <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
              <s:iterator  value="#session.BOK" status="userStatus">
                <tr style="height: 10px;">
                  <td width="65%" align="left"><s:property value="bookTitile"/></td>
                  <td width="10%" align="left"><s:property value="price"/></td>
                  <td width="10%" align="center"><s:textfield name="quantity" value="%{quantity}" size="2" /></td>
                  <td width="15%" align="center"><s:textfield value="%{price}" name="" size="6"></s:textfield></td>
                </tr>
              </s:iterator>
            </table>
          </td>
        </tr>
        <tr>
          <td height="1%" valign="top" width="100%">
            <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
              <tr style="height: 10px;">
                <td width="100%" align="right" colspan="5">
                ------------------------------------
                </td>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td height="1%" valign="top" width="100%">
            <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
              <tr style="height: 10px;">
                <td width="100%" align="right" colspan="5" style="font-weight: b">
                  <s:set var="total" value="%{0}" />
                  <s:iterator value="#session.BOK">
                    <s:set var="total" value="%{price + #attr.total}" />
                  </s:iterator>
                  <s:textfield name="subtotal" value="%{'' + #attr.total}" size="5"> </s:textfield>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </tr>
      <tr>
        <td height="1%" valign="top" width="100%">
        <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
          <tr style="height: 10px;">
            <td width="100%" align="right" colspan="5">Discount:<sj:textfield name="amt"  size="1"  placeholder=" %"/></td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td height="1%" valign="top" width="100%">
        <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
          <tr style="height: 10px;">
            <td width="100%" align="right" colspan="5">
            --------------------------------------------------------------------------------------------------
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td height="1%" valign="top" width="100%">
        <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
          <tr style="height: 10px;">
            <td width="100%" align="right" colspan="5" style="font-weight: bolder;">
              <s:set var="total" value="%{0}" />
              <s:iterator value="#session.BOK">
                <s:set var="total" value="%{price + #attr.total}" />
              </s:iterator>
              Total: <s:property value="%{'' + #attr.total}" />
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td height="1%" valign="top" width="100%">
        <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
          <tr style="height: 10px;">
            <td width="100%" align="right" colspan="5">
            --------------------------------------------------------------------------------------------------
            </td>
          </tr>
        </table>
      </td>
    </tr>
4

2 回答 2

0

用于.change()您的输入。

jQuery 文档 - 更改

于 2012-11-17T13:52:18.957 回答
0

正如@flow所说,使用.change():

$(function() {
    $('input[name^="quantity"]').change(function() {
        var unitprice = $(this).siblings('input[name^="unitprice"]').val();
        $(this).siblings('input[name^="price"]')
               .val($(this).val() * unitprice);
    });
});
于 2012-11-17T15:00:43.277 回答