在JavaScript中添加onload和onchange事件监听器
Adding onload and onchange event listeners in JavaScript
我需要添加一个onchange
和onload
事件监听器到我的JavaScript代码不使用库,但首先是我写的正确吗?我在代码中间使用了and IFFY来确保变量的隐私,我想知道在这种特殊情况下这是否有意义。
现在我想做的是使总发票显示"0€"或"已发票"与"前一次发票"之和。
我必须在哪里为Invoiced和Previous Invoiced插入onchange
事件侦听器
这里是我的提琴的链接http://jsfiddle.net/MMendes/gLsBG/
我HTML<body>
<div id="content">
<table class="register">
<caption>Register</caption>
<thead>
<tr class="heads">
<th>Date</th>
<th>Route</th>
<th>Invoiced</th>
<th>Previous Invoices</th>
<th>Total Invoiced</th>
<th>Not Collected</th>
<th>Previous Not Collected</th>
<th>Consumption Expenditures</th>
<th>Other Expenditures</th>
<th>Total Route</th>
<th>Total Collected</th>
<th>Difference T.R−T.C</th>
<th>Deposited</th>
<th>In Register</th>
</tr>
</thead>
<tbody>
<tr>
<td id="date"></td>
<td>
<input type="number" id="route" class="boxed" />
</td>
<td>
<input type="number" id="invoiced" class="boxed" />€</td>
<td>
<input type="number" id="lastinvoiced" class="boxed" />€</td>
<td id="daytotal"></td>
<td>
<input type="number" id="notcollected" class="boxed" />€</td>
<td>
<input type="number" id="notcollectedlate" class="boxed" />&euro </td>
<td>
<input type="number" id="expenditure1" class="boxed" />€</td>
<td>
<input type="number" id="expenditure2" class="boxed" />€</td>
<td id="total"></td>
<td>
<input type="number" id="totalcollected" class="boxed" />&euro</td>
<td id="difference"></td>
<td>
<input type="number" id="deposite" class="boxed" />€</td>
<td id="registered"></td>
</tr>
</tbody>
</table>
</div>
</body>
我的CSS .boxed {
border-style: none;
overflow: auto;
width: 70%;
margin: 2%;
}
body {
margin: auto;
padding: 2%, 5%, 2%, 5%;
}
caption {
font-size: 2em;
font-family:"Courier";
}
table {
border-radius: 0.5em;
border-style: solid;
background: olivedrab;
}
td {
background: darkgreen;
opacity: 0.9;
width: contain;
overflow: scroll;
}
我JS var A = {
//calculate the total invoiced for the day
calcTotalDay: function () {
//get the total invoiced for the day and set the value to 0€
var dayTotal = document.getElementById("daytotal");
if (dayTotal === "" || dayTotal === isNaN) {
dayTotal.innerHTML = "0€";
} else {
(function () {
//get the input values for invoiced and lastInvoiced "parsed as integers"
var invoiced = parseInt(document.getElementById("invoiced").value, 10);
var lastInvoiced =parseInt(document.getElementById("lastinvoiced").value, 10);
//make sure the input values are number types. If not display message demanding to insert a valid value
var type1 = typeof invoiced;
var type2 = typeof lastInvoiced;
if (type1 !== "number") invoiced.innerHTML = "Insert a valid value!";
if (type2 !== "number") lastInvoiced.innerHTML = "Insert a valid value";
//return the sum of the total by adding invoiced and lastInvoiced and adding the euro sign
if (type1 === "number" && type2 === "number") {
dayTotal = invoiced + lastInvoiced + " €";
return dayTotal;
} else {
return "invalid";
}
}());
}
}
};
在我看来,整个calcTotalDay函数(在纠正其代码之后)需要在两个字段的更改事件侦听器中运行。因此,您可以在加载事件中运行整个代码,并为运行calcTotalDay函数的相关元素添加事件侦听器。
另外,对于紧凑的跨浏览器事件侦听器添加代码,有很多例子,只需使用您最喜欢的搜索引擎。
相关文章:
- 如何使用javascript和PHP动态触发onChange事件
- Javascript:如何在onChange事件期间更改日期时间值
- JavaScript onchange事件未在IIS7中激发
- 如何在jQuery中为asp:textbox生成onchange事件
- 当触发下拉列表的onchange事件时,使用JavaScript提交表单
- 当select标记触发onChange事件时,从mysql数据库加载html表
- Javascript:OnChange事件,将数组变量传递给另一个函数
- 如何使select onchange事件在页面加载时发生
- 在onchange事件中,可以传递不是当前控件的参数吗
- OnChange事件未激发
- 此multi-onChange事件是否有效
- 如何在Jasmine/Rect中测试带有范围输入的“onChange”事件
- JSON无法处理OnChange事件
- 为什么我的onchange事件发生在页面加载时,而不是在触发时
- 如何在React's onChange事件
- JS:Onchange事件在启动时激活
- JavaScript onChange 事件侦听器对输入表单不起作用
- 如何在 PHP 中使用 OnChange 事件的下拉列表中选择一个选项后,对大量数据行设置分页
- 如何在jQuery中处理输入类型=文件的onchange事件
- Razor Dropdown onchange事件未触发始终未定义