我有一个Javascript函数,它执行以下操作:
但是,我希望在窗体下的网页上的DIV元素中显示结果,而不是在警告框中显示结果。我有什么想法可以做到这一点吗?
我的代码如下:
<script type="text/javascript">
function calc()
{
var total = 0;
var course = 0;
var nrOfLessons = 0;
var vat = 0;
course = Number(document.getElementById("course").value)
nrOfLessons = Number(document.getElementById("nrOfLessons").value)
total =(course * nrOfLessons)
vat = total * 0.15
total = total+ vat;
window.alert(total)
}
</script>
<form id="booking">
<strong>COURSE: </strong>
<select id="course">
<optgroup label="English Courses">
<option value="500">Beginner English</option>
<option value="700">Mid-Level English</option>
<option value="1000">Business English</option>
</optgroup>
<optgroup label="Thai Courses">
<option value="500">Introduction to Thai</option>
<option value="700">Pasa Thai</option>
<option value="1000">Passa Thai Mak</option>
</optgroup>
</select>
<select id="nrOfLessons">
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
提前获得所有帮助
答案 0 :(得分:11)
使用innerHTML
<script type="text/javascript">
function calc()
{
var total = 0;
var course = 0;
var nrOfLessons = 0;
var vat = 0;
course = Number(document.getElementById("course").value)
nrOfLessons = Number(document.getElementById("nrOfLessons").value)
total =(course * nrOfLessons)
vat = total * 0.15
total = total+ vat;
document.getElementById('total').innerHTML = total;
}
</script>
<div id="total"></div>
答案 1 :(得分:4)
为您的<div>
提供ID,例如......
<div id="resultDiv"></div>
然后在您的javascript中设置.innerHTML
属性...
document.getElementById("resultDiv").innerHTML = total.toString();
答案 2 :(得分:1)
你可以使用
<div id="resultDiv"></div>
document.getElementById("resultDiv").innerHTML = total.toString();
答案 3 :(得分:1)
$(document).ready(function(){
$("#serachimg").click(function(){
$("#search").slideToggle();
});
});
function content(){
var div = document.getElementById("textDiv");
div.textContent = "my text";
var text = div.textContent;
var div = document.getElementById("textDiv");
div.style="font-size:24px";
}
<div id="textDiv"><script> content();</script></div>
答案 4 :(得分:-2)
window.onload = function () {
var display = document.querySelector('#total');
total(display);
};
<div>The grand total is <span id="total"></span></div>