javascript - 在外部 Div 中渲染价格

标签 javascript html

我找到了一个代码,可以根据影响最终价格的表单输入(复选框)中的一些选择来显示最终价格。我还希望它影响带有文本的外部 div。是我创建的一个巨大的固定元素,显示“最终成本”,我还希望它通过更改 getElementByID 来呈现最终价格,但没有任何反应。你能帮我解决这个问题吗?

我也希望 .priceText1 也受到影响:)

实际代码是

HTML

 <form action="" id="theForm">
    <fieldset>
        <legend>
            Products
        </legend>
        <label>
            <input name="product" value="12.95" type="checkbox" id="p1" onclick="totalIt()"/>
            Candy $12.95
        </label>
        <label>
            <input name="product" value="5.99" type="checkbox" id="p2" onclick="totalIt()"/>
            Burger $5.99
        </label>
        <label>
            <input name="product" value="1.99" type="checkbox" id="p3" onclick="totalIt()"/>
            Coke $1.99
        </label>
        <label>
            Total 
            <input value="$0.00" readonly="readonly" type="text" id="total"/>
        </label>
    </fieldset>
    <input value="Submit" type="submit"/>
    <input value="Reset" type="reset"/>
</form>

<div class="priceWrapper">
            <h3 class="priceText1">$0.00</h3>
            <h3 class="priceText2">Final Cost</h3>
</div>

JS

function totalIt() {
    var input = document.getElementsByName("product");
    var total = 0;
    for (var i = 0; i < input.length; i++) {
        if (input[i].checked) {
          total += parseFloat(input[i].value);
        }
    }

    document.getElementById("total").value = "$" + total.toFixed(2);
}

最佳答案

您可以简单地使用 innerTextquerySelector 将总价添加到元素 .priceText1 中,例如:

document.querySelector(".priceText1").innerText = "$" + total.toFixed(2);

希望这对您有所帮助。

function totalIt() {
  var input = document.getElementsByName("product");
  var total = 0;
  for (var i = 0; i < input.length; i++) {
    if (input[i].checked) {
      total += parseFloat(input[i].value);
    }
  }
  document.getElementById("total").value = "$" + total.toFixed(2);
  document.querySelector(".priceText1").innerText = "$" + total.toFixed(2);
}
<form action="" id="theForm">
  <fieldset>
    <legend>
      Products
    </legend>
    <label>
            <input name="product" value="12.95" type="checkbox" id="p1" onclick="totalIt()"/>
            Candy $12.95
        </label>
    <label>
            <input name="product" value="5.99" type="checkbox" id="p2" onclick="totalIt()"/>
            Burger $5.99
        </label>
    <label>
            <input name="product" value="1.99" type="checkbox" id="p3" onclick="totalIt()"/>
            Coke $1.99
        </label>
    <label>
            Total 
            <input value="$0.00" readonly="readonly" type="text" id="total"/>
        </label>
  </fieldset>
  <input value="Submit" type="submit" />
  <input value="Reset" type="reset" />
</form>

<div class="priceWrapper">
  <h3 class="priceText1">$0.00</h3>
  <h3 class="priceText2">Final Cost</h3>
</div>

关于javascript - 在外部 Div 中渲染价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47227906/

相关文章:

javascript - 使用loopback.io创建一个新项目

javascript - css - 在找到行后突出显示 td 元素

javascript - 将 Json 数据作为单个对象而不是一个长数组推送

php - 如何摆脱已关闭浏览器或选项卡的用户

php - 检查电子邮件内容是纯文本还是 HTML - PHP

html - 为什么我们使用相对方案//而不是 http ://or https://?

javascript - 使用 jQuery 查找美元符号并将其替换为卢比符号

javascript - 如何使用 javascript 获取完整日期?

javascript - 此代码块的 eval() 函数的替代方案

javascript - 使用 jQuery 打开和关闭类