简体   繁体   中英

Display javascript value in html

I'm hoping someone can help, I'm new to Javascript but not HTML/CSS. I'm having a small issue trying to display some Javascript values onto my HTML page. So in a nutshell:

I have two form range sliders the values from these inputs then use JavaScript to work out the total including interest (it's a loan calculator). However what I need is to display the sum at the bottom of the form so:

Amount Borrowed [value] + Interest [value] = Total repay [value]

I need this to update the values as they are sliding the input range, I can't have the user needing to click a button.

Here is my JS:

<script type="text/javascript"><!--
function updatesum() {
document.form.sum.value = (document.form.borrow.value -0 + 5.5) / 100 * 
(document.form.duration.value -0) + (document.form.borrow.value -0 + 5.5);
}
//--></script>

My HTML:

<form name="form" class="form">
    <label for="borrow">How much would you like to <span>borrow</span>?</label>
    <input type="range" name="borrow" id="borrow" value="150" min="1" max="400" onChange="updatesum()" style="color: #1271a9;">

    <label for="duration">How <span>long</span> for?</label>
    <input type="range" name="duration" id="duration" value="18" min="1" max="38" onChange="updatesum()" style="color: #1271a9;">
    <label>Total Repayable:</label>
    <input name="sum" readonly  style="color: #1271a9; font-weight: bold;">
    <p class="sum">Borrowing <div id="sumBorrow"><span>[sum]</span></div> + Interest & fees <span name="duration">[sum]</span> = Total to repay <span name="sum">[sum]</span></p>
  </form>

You can use jQuery and some minor html changes

<form name="form" class="form">
    <label for="borrow">How much would you like to <span>borrow</span>?</label>
    <input type="range" name="borrow" id="borrow" value="150" min="1" max="400" onChange="updatesum()" style="color: #1271a9;" />
    <label for="duration">How <span>long</span> for?</label>
    <input type="range" name="duration" id="duration" value="18" min="1" max="38" onChange="updatesum()" style="color: #1271a9;" />
    <label>Total Repayable:</label>
    <input name="sum" id="sum" readonly style="color: #1271a9; font-weight: bold;" />
    <p class="sum">Borrowing
        <div id="sumBorrow"><span class="amount">[sum]</span>
        </div>+ Interest & fees <span class="interest">[sum]</span> = Total to repay <span class="total">[sum]</span>
    </p>
</form>

then

function updatesum() {
    var amount = +$('#borrow').val() || 0,
        duration = +$('#duration').val() || 0,
        interest = (amount + 5.5) / 100 * duration,
        payable = interest + (amount + 5.5);
    $('#sum').val(payable.toFixed(2));
    $('.amount').text(amount.toFixed(2));
    $('.interest').text(interest.toFixed(2));
    $('.total').text(payable.toFixed(2));
}

Demo: Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM