简体   繁体   中英

Getting javascript variable value inside html

I have the following piece of javascript code in my project.

 <script type="text/javascript"> function changeval() { total = parseInt($("#small").val()) + parseInt($("#medium").val()) + parseInt($("#large").val()) + parseInt($("#xlarge").val()) + parseInt($("#xxlarge").val()); } 

How can I get the value of $total in the following code instead of 12 in the last line?

<tr>
    <td><b>Total</b></td>
    <td>
        <input id="total" name="total" type="number" value="1" class="form-control total input-md" disabled min=1 max=99999 />
    </td>
    <td> <div class="total-quantity" style="display:none;">12</div> </td>
</tr>

add the total to the element

function changeval() {
        $total = parseInt($("#small").val()) + parseInt($("#medium").val()) + parseInt($("#large").val()) + parseInt($("#xlarge").val()) + parseInt($("#xxlarge").val());
        $('.total-quantity').text($total);
}

Replace 12 with something like a label and give it an id. Let's call the id="totalQty".

In the Javascript $totalQty.val() = 12 or what ever you want to set it to.

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