2015-10-27 194 views
0

我是新的bootstrap編碼。我有一個簡單的問題,但我找不到答案。計算行內總價值

這就是我需要的:我有單件重量,我需要計算總重量的單位數。

有人可以幫助我嗎?

<div class="col-xs-4 control-group form-group"> 
    <div class="controls"> 
     <label>Unit Weight:</label> 
     <input type="text" class="form-control" id="uweight" required data-validation-required-message="Please enter your name."> 
     <p class="help-block"></p> 
    </div> 
</div> 
<div class="col-xs-4 control-group form-group"> 
    <div class="controls"> 
     <label>Units:</label> 
     <input type="text" class="form-control" id="units" required data-validation-required-message="Please enter your name."> 
     <p class="help-block"></p> 
    </div> 
</div> 
<div class="col-xs-4 control-group form-group"> 
    <div class="controls"> 
     <label>Total:</label> 
     <input type="text" class="form-control" id="total" > 
     <p class="help-block"></p> 
    </div> 
</div> 
+0

單程辦法http://jsfiddle.net/ny74j496/1/ – Shehary

+0

嗨Ottavio,非常感謝您的及時回覆。這可以用[計算]按鈕完成嗎?用戶去巢場總計自動計算? – Risa

+0

你的意思是這樣的http://jsfiddle.net/ny74j496/4/ – Shehary

回答

0

您可以使用jQuery將您的輸入的值與他們的ID一起拉動,然後執行您的計算。您只需爲每個輸入ID和計算分配一個變量,然後輸出到#total

*我已將您的輸入類型從text也更改爲number

$(document).ready(function() { 
 
    $(document).on('change', '.form-control', function() { 
 
    var yourWeight, yourUnits, yourTotal; 
 
    yourWeight = $(' #uweight ').val(); 
 
    yourUnits = $(' #units ').val(); 
 
    yourTotal = (parseFloat(yourWeight)) * (parseFloat(yourUnits)); 
 
    $(' #total ').val(yourTotal.toFixed(2)); 
 
    }); 
 
});
body { 
 
    padding-top: 50px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> 
 
<div class="container"> 
 
    <form name="calcForm"> 
 
    <div class="row"> 
 
     <div class="col-xs-4 control-group form-group"> 
 
     <div class="controls"> 
 
      <label>Unit Weight:</label> 
 
      <input type="number" class="form-control" id="uweight" required data-validation-required-message="Please enter your name."> 
 
      <p class="help-block"></p> 
 
     </div> 
 
     </div> 
 
     <div class="col-xs-4 control-group form-group"> 
 
     <div class="controls"> 
 
      <label>Units:</label> 
 
      <input type="number" class="form-control" id="units" required data-validation-required-message="Please enter your name."> 
 
      <p class="help-block"></p> 
 
     </div> 
 
     </div> 
 
     <div class="col-xs-4 control-group form-group"> 
 
     <div class="controls"> 
 
      <label>Total:</label> 
 
      <input type="number" class="form-control" id="total" disabled/> 
 
      <p class="help-block"></p> 
 
     </div> 
 
     </div> 
 
    </div> 
 

 
    </form> 
 
</div>

+0

這是不必要的矯枉過正解決方案,以上的OP要飛行時計算輸入值,沒有任何按鈕來計算,檢查OP評論下面的問題,旁註:很大的努力:) – Shehary

+0

嗨,對不起,我把評論發送到錯誤的地方。 – Risa

+0

是的,最後的代碼好多了,只有一個我不得不改變,所以填寫表格後我可以正確發送。 id =「total」禁用應該更改爲只讀。 – Risa

0

添加calculate類給每個輸入後面jQuery keyup function結合,當用戶釋放鑰匙,計算可以在運行時被完成,和將輸出到#total

$(document).ready(function() { 
 

 
    //Calculate both inputs value on the fly 
 
    $('.calculate').keyup(function() { 
 
     var Tot = parseFloat($('#units').val()) + parseFloat($('#uweight').val()); 
 
     $('#total').val(Tot); 
 
    }); 
 

 
    //Clear both inputs first time when user focus on each inputs and clear value 00 
 
    $('.calculate').focus(function (event) { 
 
     $(this).val("").unbind(event); 
 
    }); 
 

 
    //Remove this it's just for example purpose 
 
    $('.calculate').keypress(function (e) { 
 
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) { 
 
      $("#errmsg").html("Digits Only").show().fadeOut("slow"); 
 
      return false; 
 
     } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" /> 
 
<div class="col-xs-4 control-group form-group"> 
 
    <div class="controls"> 
 
     <label>Unit Weight:</label> 
 
     <input type="text" class="form-control calculate" id="uweight" required value="00"> 
 
     <p class="help-block"></p> 
 
    </div> 
 
</div> 
 
<div class="col-xs-4 control-group form-group"> 
 
    <div class="controls"> 
 
     <label>Units:</label> 
 
     <input type="text" class="form-control calculate" id="units" required value="00"> 
 
     <p class="help-block"></p> 
 
    </div> 
 
</div> 
 
<div class="col-xs-4 control-group form-group"> 
 
    <div class="controls"> 
 
     <label>Total:</label> 
 
     <input type="text" class="form-control" id="total" readonly> 
 
     <p class="help-block"></p> 
 
    </div> 
 
</div> 
 
<div class="col-xs-12 control-group form-group" id="errmsg"></div>

Fiddle

+0

您瞭解我的回答是在OP發表評論之前發佈的嗎?你的答案不允許使用十進制符號(ei 1.5)(是的,我確實看到要刪除的評論,但它通常沒有什麼價值),我不相信體重+單位=總重量,它應該是單位重量MULTIPLIED(*)。 – vanburen

+0

@vanburen,可以回答你的評論,但它會開始新的辯論,所以,你是對的,你的答案比我的好得多,你可以贏得冠軍盃,歡呼聲 – Shehary