javascript - 从 JavaScript 变量更新 HTML

标签 javascript html css

我正在尝试构建一个表单,其中包含用户选择的一些数字类型输入和一个复选框。 我没有使用 php 它所有的 java 因为我试图使用他的输入来计算账单并打印出结果。 我如何打印这些变量? document.write 不会这样做,因为它会在没有计算的情况下立即打印 var。

这是一些脚本(没什么):

$(document).ready(function(e) {
    $('.content_window').css('height','900');
    $('#top').css('float','none');
    var shutter_price
    shutter_price = ($('#shutter').val())*200;  
    if (shutter != '0' )
        write(shutter_price);   
});

和表单的输入:

<label for="shutter">shutter </label> 
<input style="margin-right:98px" name="shutter" type="number" id="shutter">

有什么建议吗?

最佳答案

与其 document.write 不如在页面上更新或添加一个元素。

例如,修改您提供的代码和标记:

Javascript (jQuery)

$(document).ready(function(e) {
    $('.content_window').css('height','900');
    $('#top').css('float','none');

    $('#shutter').on("change", function(e) {
        var shutter_price = $(this).val()*200;
        if (shutter_price >= 0) {
            $('#shutter-result').text(shutter_price);
        }
    });
});

HTML

<label for="shutter">shutter </label>
<input style="margin-right:98px" name="shutter" type="number" id="shutter" min="0" step="1" pattern="\d+" />

<div id="shutter-result"></div>

JSFiddle

关于javascript - 从 JavaScript 变量更新 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22545006/

相关文章:

javascript - 更新对象数组中值的更好方法

html - 带有空白的 Flexbox 容器无法正常工作

php - 使用 PHP 将 MYSQL 查询中的元素添加到 HTML 表

html - 如何在同一行中排列两个不同id的div?

javascript - 使用 Python 单击 Selenium 中的 javascript 链接不起作用

javascript - 解析字符串并删除空格和换行符并转换为 json OBJECT

html - 在 HTML/CSS 中使用自定义字体

html - 图像上文本的CSS相对定位

javascript - 显示更多/显示更少 Jquery 代码可以在 fiddle 上运行,但不能在我的代码中运行

javascript - 数据以凌乱的方式加载到页面上