【问题标题】:jQuery- Dynamically multiply input values of table rows with different idjQuery-动态地将具有不同id的表行的输入值相乘
【发布时间】:2015-08-18 05:56:26
【问题描述】:

使用以下代码动态添加表格行。输入 ID 附加用户 ID。

var selResId = jQuery('#grid').jqGrid('getGridParam', 'selarrrow');

    var j=1;
    for (var i=0, il=selResId.length; i < il; i++) {
        var name = jQuery('#grid').jqGrid('getCell', selResId[i], 'USER_NAME');

    $('#addr'+j).html("<td style='text-align:center;'>"+name+"</td><td><input id='hours_"+selResId[i]+"' value='80' type='text' readonly  /></td><td><input id='rate_"+selResId[i]+"' type='text' /></td><td><input name='markup_"+selResId[i]+"' type='text'/></td><td><input name='totalcost_"+selResId[i]+"' type='text' readonly></td>");
    $('#resource_table').append('<tr id="addr'+(j+1)+'"></tr>');
        j++;
        }
    }

HTML 生成

<tr id="addr1">
    <td>John Doe</td>
    <td><input type="text" readonly="" value="80" id="hours_10"></td>
    <td><input type="text" value="" id="rate_10"></td>
    <td><input type="text" value="" id="markup_10"></td>
    <td><input type="text" readonly="" value="" id="totalcost_10"></td>
</tr>

<tr id="addr2">
    <td>Foo User</td>
    <td><input type="text" readonly="" value="80" id="hours_11"></td>
    <td><input type="text" value="" id="rate_11"></td>
    <td><input type="text" value="" id="markup_11"></td>
    <td><input type="text" readonly="" value="" id="totalcost_11"></td>
</tr>

如何将hours, rate and markup 的输入值相乘,并使用以下公式将其显示在总成本输入下。该事件可能是keyup

Initially, totalcost = hours * rate

Case 1: If markup (%) > 0, for eg: 10%, then markup_cost = (hours * rate * markup) / 100
totalcost = (hours * rate) + markup_cost

Case 2: If markup (%) < 0, for eg: -10%, then markup_cost = (hours * rate * markup) / 100
totalcost = (hours * rate) - markup_cost

【问题讨论】:

    标签: javascript jquery html jqgrid


    【解决方案1】:

    尽量用starts with selector点赞,

    $(function(){
       function setTotalCost(n){
           var h=Number($('#hours_'+n).val()),
               m=Number($('#markup_'+n).val()), // taking 0 if empty
               r=Number($('#rate_'+n).val());
           $('#totalcost_'+n).val(h*m*r);
       }
    
       $('[id^="rate_"]').on('keyup',function(){
           var n = this.id.replace('rate_','');// get the number
           setTotalCost(n);   
       });
       $('[id^="markup_"]').on('keyup',function(){
           var n = this.id.replace('markup_','');// get the number
           setTotalCost(n);   
       });
    });
    

    $(function(){
       function setTotalCost(n){
           var h=Number($('#hours_'+n).val()),
               m=Number($('#markup_'+n).val()), // taking 0 if empty
               r=Number($('#rate_'+n).val());
           $('#totalcost_'+n).val(h*m*r);
       }
       
       $('[id^="rate_"]').on('keyup',function(){
           var n = this.id.replace('rate_','');// get the number
           setTotalCost(n);   
       });
       $('[id^="markup_"]').on('keyup',function(){
           var n = this.id.replace('markup_','');// get the number
           setTotalCost(n);   
       });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
      <tr id="addr1">
        <td>John Doe</td>
        <td>
          <input type="text" readonly="" value="80" id="hours_10">
        </td>
        <td>
          <input type="text" value="" id="rate_10">
        </td>
        <td>
          <input type="text" value="" id="markup_10">
        </td>
        <td>
          <input type="text" readonly="" value="" id="totalcost_10">
        </td>
      </tr>
    
      <tr id="addr2">
        <td>Foo User</td>
        <td>
          <input type="text" readonly="" value="80" id="hours_11">
        </td>
        <td>
          <input type="text" value="" id="rate_11">
        </td>
        <td>
          <input type="text" value="" id="markup_11">
        </td>
        <td>
          <input type="text" readonly="" value="" id="totalcost_11">
        </td>
      </tr>
    </table>

    【讨论】:

    • 这里,hours 输入将是只读的,因此将调用 keyup 来获取速率和标记,然后计算 totalcost
    • 代码看起来是合法的,但似乎没有根据费率或标记输入乘以值,从而导致总成本。我也在 keyup 功能后尝试了 console.log('Test') ,但它似乎没有工作。请检查这个小提琴 - jsfiddle.net/5nk3g45y
    • 最后我为ratemarkup添加了keyup事件。
    • 实际上,标记可能是空的。 OI 已针对某些用例修改了我的问题。 Initially, totalcost = hours * rate Case 1: If markup (%) &gt; 0, for eg: 10%, then markup_cost = (hours * rate * markup) / 100 totalcost = (hours * rate) + markup_cost Case 2: If markup (%) &lt; 0, for eg: -10%, then markup_cost = (hours * rate * markup) / 100 totalcost = (hours * rate) - markup_cost
    • 我认为你的逻辑很清楚,我的代码足以实现你的逻辑。所以,用我的代码试试你的逻辑,如果你有任何问题,那么你可以在这里发布。
    猜你喜欢
    • 2014-04-24
    • 2022-08-23
    • 2019-12-10
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 2019-07-09
    相关资源
    最近更新 更多