Javascript:计算余额并通过文本字段输入前进

时间:2016-06-28 11:26:52

标签: javascript

我是JavaScript的初学者。我有代码,用户通过输入字段输入值('输入给定的费用')。我想要的是,如果用户输入的数字超过“1000”,那么预付值应该出现在另一个输入文本中(“预付费”)。

如果用户输入的内容小于'1000',那么余额应该是另一个文字输入('余额费')。

我的Fiddle带有我工作的方式。

我的Html表单是:

 <form>
<div class="tbl_admission_FormCell">Given Fees</div> 
  <div class="tbl_admission_FormCell" style="padding:10px;"><input type="text" name="given_fees" value="" id="given_fees" onkeypress="givenfees()" onkeyup="givenfees()"/> </div> 

<div class="tbl_admission_FormCell">Balance Fees</div> 

<div class="tbl_admission_FormCell"><span id="balance_fees"></span> </div>

 <div class="tbl_admission_FormCell">Advance Fees</div> 

 <div class="tbl_admission_FormCell"><span id="advance_fees"></span> </div>
 <div class="tbl_admission_FormRow"> 
  <div class="tbl_admission_FormCell">Total Fees</div>
    <div class="tbl_admission_FormCell"><input type="text" id="total_fees" name="total_fees" style="border:none;" value="1000" readonly/></div> 
  <div class="tbl_admission_FormCell"><div class="amt_total" onchange="amt_total()" ></div></div>
  </div>
</form>
<script>
function givenfees(){
    var given_fees = document.getElementById("given_fees");
    var s = given_fees.value;
    bal_value = +s - +result;       
    var balance_fees = document.getElementById("balance_fees");
    if(+bal_value<0 )
    {
    advance_fees.innerText = +bal_value;
    }
    else
    {balance_fees.innerText = +bal_value;}
}
</script>

2 个答案:

答案 0 :(得分:2)

我已经编辑了你的答案。请检查fiddle。希望如果我完全理解你的问题,这将对你有帮助。

function givenfees(){ 
    var result=1000;
    var given_fees = document.getElementById("given_fees");
    var s = given_fees.value;
    bal_value = +s - +result;    
    var balance_fees = document.getElementById("balance_fees");
    if(+bal_value>0 )
    {
    advance_fees.innerText = +bal_value;
    balance_fees.innerText = 0;
    }
    else
    {
    balance_fees.innerText = +bal_value;
    advance_fees.innerText = 0;
    }
}

答案 1 :(得分:-1)

继续致电&#39; calValue&#39;函数onchange input1

function calValue(){
  var input1 = document.getElementById("input1").value;
  var input2 = document.getElementById("input2");
  if(input1 >= 1000){
    input2.value = input1 - 1000;
  }else{
    input2.value = 1000 - input1;
  }
}