简体   繁体   English

将addEventListener与对象文字一起使用

[英]Using addEventListener with object literal

I am trying to write my JS using the Good Parts as described by Douglas Crockford and therefore created a single global object A . 我试图使用Douglas Crockford所描述的Good Parts编写JS,因此创建了一个全局对象A However I am having trouble with my JS code. 但是我在我的JS代码方面遇到了麻烦。 The variables value1 and value2 are evaluating to NaN and I don't understand why. 变量value1value2的计算结果为NaN ,我不明白为什么。

I have declared the two variables invoiced and lastInvoiced outside of the totalInvoiced and then tried to get them inside the method using this . 我已经宣布了两个变量invoicedlastInvoiced的外totalInvoiced ,然后试图让他们使用方法内部this However, I don't know if this is correct? 但是,我不知道这是否正确? I did this in the hope that I could then use addEventListener on those variables and call the onchange event. 我这样做是希望可以在这些变量上使用addEventListener并调用onchange事件。

Why are value1 and value2 evalutaing to NaN ? 为什么value1value2评估为NaN

How would I write the code to use 'addEventListener' with the onchange event on those two variables? 我该如何编写代码来在这两个变量的onchange事件上使用“ addEventListener”?

Here is a jsFiddle link to all of my code. 这是我所有代码的jsFiddle链接。

This is my JS code: 这是我的JS代码:

var A = {

invoiced: document.getElementById("invoiced").value,   
lastInvoiced: document.getElementById("lastinvoiced").value,
totalInvoiced: function () {

 var invoiced1 = this.invoiced;
 var lastInvoiced1 = this.lastInvoiced;
 var value1 = parseFloat("invoiced1");
 var value2 = parseFloat("lastInvoiced1");

     if(isNaN(value1))
         value1 = 0;
     if(isNaN(value2))
         value2 = 0;

 var totalInvoiced1 = value1 + value2;
 var value3 = document.getElementById("daytotal").value = totalInvoiced1 + "€";
 return value3;   
 }

 };
window.onload = A.totalInvoiced();
A.invoiced.addEventListener("change", A.totalInvoiced, false);
A.lastInvoiced.addEventListener("change", A.totalInvoiced, false);

Firstly, parseFloat("invoiced1"); 首先, parseFloat("invoiced1"); and parseFloat("lastInvoiced"); parseFloat("lastInvoiced"); are evaluating the strings invoiced1 , lastInvoiced - not the variables - change that, so: 是评估 invoiced1lastInvoiced -不变量-改变这种状况,那么:

parseFloat(invoiced1);
parseFloat(lastInvoiced);

Secondly, the <input> elements you are attempting to get the values from don't actually have any values set. 其次,您尝试从中获取值的<input>元素实际上没有设置任何值。

  <td><input type="number" id="invoiced" class="boxed" />&euro;</td>

Here, &euro; 在这里, &euro; is the value of the <td> , not the <input> . <td>而不是<input> You should give it an initial value - you shouldn't need to test for NaN in your code if you write it properly, so remove that check. 您应该给它一个初始值-如果正确编写,则不需要在代码中测试NaN ,因此请删除该检查。


Thirdly, as mentioned, you're setting the events to the element's value, not the element - try setting them to the elements themselves, and then using A.invoiced.value; 第三,如上所述,您将事件设置为元素的值,而不是元素-尝试将事件设置为元素本身,然后使用A.invoiced.value; etc (use A instead of this as this will sometimes refer to different objects in your context, as you are calling it in different ways) instead to grab the value, eg: 等(使用A ,而不是this因为this有时会引用不同的对象在你的情况下,当你以不同的方式调用它)而不是抢值,例如:

invoiced: document.getElementById("invoiced"),
lastInvoiced: document.getElementById("lastinvoiced"),
totalInvoiced: function () {
    var invoiced = A.invoiced.value;
    var lastInvoiced = A.lastInvoiced.value; 
    ...

Here's an updated jsFiddle example 这是一个更新的jsFiddle示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM