2017-01-18 26 views
-1

我有代碼的jQuery表,我需要使用,如果其他條件與表的最後一個字段m3只有結果值&條件是如果結果大於(>)7然後顯示「FCL」下面的表,如果小於( <)7比「LCL」要高,如果m3列中沒有結果值意味着最後一個字段m3的表結果爲空,則不應顯示任何內容。如何使用jQuery表字段導致if else條件?

<script type="text/javascript"> 
    $(function() { 
     $('.pnm, .price, .subtot, .widtot, .perm, .tottot, .vol, .tot, .vols, .widths, .acts').prop('readonly', true); 
     var $tblrows = $("#tblProducts tbody tr"); 

     $tblrows.each(function (index) { 
      var $tblrow = $(this); 
      $tblrow.find('.width, .carton, .perm').on('change', function() { 
       var carton = $tblrow.find("[name=carton][type=number][min=0]").val(); 
       var width = $tblrow.find("[name=width][type=number][min=0]").val(); 
        var depth = $tblrow.find("[name=depth][type=number][min=0]").val() 
        var perm = $tblrow.find("[name=perm]").val(); 

       var subTotal =parseFloat(width*0.01, 10) * parseInt(carton, 10); 
       var cartons =parseInt(carton, 10); 
       var widths =parseInt(width, 10); 

       if (!isNaN(cartons)) { 

        $tblrow.find('.carton').val(cartons.toFixed(0)); 
        var cartonTotal = 0; 

        $(".carton").each(function() { 
         var stval = parseInt($(this).val()); 
         cartonTotal += isNaN(stval) ? 0 : stval; 
        }); 

        $('.cartontot').val(cartonTotal.toFixed(0)); 
       }   
       if (!isNaN(widths)) { 

        $tblrow.find('.width').val(widths.toFixed(0)); 
        var widthTotal = 0; 

        $(".width").each(function() { 
         var stval = parseInt($(this).val()); 
         widthTotal += isNaN(stval) ? 0 : stval; 
        }); 

        $('.widthtot').val(widthTotal.toFixed(0)); 
       } 
       if (!isNaN(subTotal)) { 

        $tblrow.find('.perm').val(subTotal.toFixed(5)); 
        var grandTotal = 0; 

        $(".perm").each(function() { 
         var stval = parseFloat($(this).val()); 
         grandTotal += isNaN(stval) ? 0 : stval; 
        }); 

        $('.grdtot').val(grandTotal.toFixed(5)); 
       } 
      }); 
     }); 
    }); 
</script> 

<table id="tblProducts"> 
<thead> 
    <tr> 
     <td><strong>Products</strong></td> 
     <td><strong>Cartons</td> 
     <td><strong>Cm Width</td> 
     <td><strong>M3</td> 
    </tr> 
</thead> 
<tbody> 
    <tr> 
     <td><input type="text" class="pnm" value="Product One" name="pnm" style="width:120px" /></td> 
     <td ><input type="number" oninput="validity.valid||(value='');" class="carton" value="0" name="carton" min="0" maxlength="5" style="width:70px"></td> 
     <td><input type="number" oninput="validity.valid||(value='');" class="width" value="0" min="0" name="width" maxlength="5" style="width:80px"/></td> 
     <td><input type="number" class="perm" value="" name="perm" style="width:80px"/></td> 
     <td class="error" style="color:red"></td>   
    </tr> 
    <tr> 
     <td><input type="text" class="pnm" value="Product Second" name="pnm" style="width:120px" /></td> 
     <td ><input type="number" oninput="validity.valid||(value='');" class="carton" value="0" name="carton" min="0" maxlength="5" style="width:70px"></td> 
     <td><input type="number" oninput="validity.valid||(value='');" class="width" value="0" min="0" name="width" maxlength="5" style="width:80px"/></td> 
     <td><input type="number" class="perm" value="" name="perm" style="width:80px"/></td> 
     <td class="error" style="color:red"></td>   
    </tr> 
</tbody> 
<tfoot> 
    <tr> 
     <td></td> 
     <td><input type="number" class="cartontot" value="" name="" style="width:70px" readonly/></td> 
     <td></td> 
     <td><input type="number" class="grdtot" value="" name="" style="width:80px" readonly/></td> 
    </tr> 
</tfoot> 
</table> 

回答

0

您已具有使您的狀況發生變化的價值,它在您的grandTotal變量中。所以你必須基於你的條件!我在表格中添加一行以顯示消息。

<tr> 
    <td></td> 
    <td></td> 
    <td></td> 
    <td id="result">result: </td> 
</tr> 

和幾行代碼顯示消息。

if (grandTotal > 7) { 
        $('#result').html('FCL'); 
       } else if (grandTotal < 7) { 
        $('#result').html('LCL'); 
       } 

並且這裏是fiddle

+0

是的非常感謝你的工作。非常感謝 。 先生我有關於此代碼的另一個問題,如果可以回答我 ,這裏是問題https://jsfiddle.net/LvzjLp06/14/ – user7428613

+0

沒問題。高興可以幫助:) @ user7428613 – moay

+0

先生,我有關於此代碼的另一個問題,如果能回答我 ,這裏是問題https://jsfiddle.net/LvzjLp06/17/ 所以問題是在這裏,條件是隻應用於高度欄,如果其他情況下顯示錯誤,如果條件將是錯誤的,但它得到的問題是,如果我輸入滿足高度條件的值,則它得到反映該錯誤。所以我需要該條件應該只與高度列值不能反映與其他列values.can你請檢查先生。請儘快回覆我,謝謝,並感謝問候 – user7428613