简体   繁体   English

JavaScript 函数结果不一致

[英]Inconsistent JavaScript Function Results

I have this function in my Javascript Code that updates html fields with their new values whenever it is called.我在我的 Javascript 代码中有这个函数,每当它被调用时,它就会用它们的新值更新 html 字段。 The problem cannot be with the function itself because it works brilliantly in every section except for one.问题不在于函数本身,因为它在每个部分都表现出色,除了一个部分。 Here is the JS function:这是JS函数:

  function updateFields() {
    document.getElementById('bf').innerHTML = bill.time[breakfast][bill.pointPartOfWeek];
    document.getElementById('ln').innerHTML = bill.time[lunch][bill.pointPartOfWeek];
    document.getElementById('dn').innerHTML = bill.time[dinner][bill.pointPartOfWeek];
    document.getElementById('se').innerHTML = bill.time[special][bill.pointPartOfWeek];
    document.getElementById('fdr').innerHTML = bill.time[full][bill.pointPartOfWeek];
    document.getElementById('cost').innerHTML = bill.cost;
  }

And it executes fine in the following instance:它在以下实例中执行良好:

  <select onchange='if(this.selectedIndex == 0) {bill.unholiday();updateFields()} else { bill.holiday();updateFields()}' id='date' name='date'>
    <option value='else'>Jan. 02 - Nov. 20</option>
    <option value='christmas'>Nov. 20 - Jan. 01</option>
  </select>

but in this very similar code, the last line of the function doesn't seem to execute (it doesn't update the cost field, but updates everything else)但是在这个非常相似的代码中,函数的最后一行似乎没有执行(它没有更新成本字段,而是更新其他所有内容)

  <select onchange='if(this.selectedIndex == 0) {bill.pointPartOfWeek = 1;} else { bill.pointPartOfWeek = 2;}updateFields();alert(updateFields());' id='day' name='day'>
    <option value='0'>Monday thru Thursday</option>
    <option value='1'>Friday, Saturday, or Sunday</option>
  </select>
  <br />

Strangely enough, the total cost variable itself is updated, but the field that represents the variable is not.奇怪的是,总成本变量本身会更新,但表示变量的字段却没有。 If you use another section of the page that wouldn't change the value of the total cost but calls the updateFields function again, the cost field then updates correctly.如果您使用不会更改总成本值但再次调用 updateFields 函数的页面的另一部分,则成本字段会正确更新。 It must be an issue with the function called.一定是调用的函数有问题。

Note: we know that the function executes because it does 5 out of 6 of the things it is supposed to do.注意:我们知道该函数执行是因为它完成了它应该做的 6 件事中的 5 件事。 This is a strange issue.这是一个奇怪的问题。

Edit: The pastebin for the entire page my be helpful.编辑:整个页面的 pastebin 我是有帮助的。 Here it is:这里是:

http://pastebin.com/f70d584d3 http://pastebin.com/f70d584d3

I'm curious, is it possible that there are actually 2 elements with an id of "cost"?我很好奇,是否有可能实际上有 2 个元素的 ID 为“成本”? That could, by updating the first one it finds, cause this issue.通过更新它找到的第一个,这可能会导致此问题。 Different browsers may have different ways of implementing document.getElementById() so you might get even more inconsistent results with different browsers if this is the case.不同的浏览器可能有不同的实现 document.getElementById() 的方式,因此如果是这种情况,您可能会在不同的浏览器中得到更加不一致的结果。

UPDATE: It turns out that you need to call bill.holiday() or bill.unholiday() before calling updateFields() in your second select.更新:事实证明,您需要在第二次选择中调用 updateFields() 之前调用 bill.holiday() 或 bill.unholiday()。

My experience with getElementById has been mixed at best, and that's most likely your issue.我对 getElementById 的经验充其量是混合的,这很可能是您的问题。 Probably some sort of browser DOM bug where you've got two items with the same ID, or something like that.可能是某种浏览器 DOM 错误,其中您有两个具有相同 ID 的项目,或者类似的问题。

I use Prototype which lets you avoid ambiguities and finicky browsers with a simple call:我使用Prototype ,它可以让您通过一个简单的调用来避免歧义和挑剔的浏览器:

document.getElementById('bf')

becomes变成

$('bf')

And similarly you can update the innerHTML of an element easily:同样,您可以轻松更新元素的innerHTML:

$('bf').update(bill.time[breakfast][bill.pointPartOfWeek]);

Check it out.一探究竟。 It's saved my bacon more than a couple times.它多次救了我的培根。

The problem is that the cost property on the billiard object has not been updated when you call updateFields().问题是当您调用 updateFields() 时,台球对象上的成本属性尚未更新。 You need to call bill.calculate() which updates the cost property.您需要调用 bill.calculate() 来更新成本属性。

I understand now why Eric's solution worked... which lead me to a better solution.我现在明白为什么 Eric 的解决方案有效……这让我找到了一个更好的解决方案。

His post was deleted for some reason, but I commented on his other post paraphrasing his answer.由于某种原因,他的帖子被删除了,但我在他的另一篇帖子中评论了他的回答。

Anyways, He said to call holiday() or unholiday().无论如何,他说要打电话给holiday() 或unholiday()。 When you look at those functions you'll see on the last line of code:当您查看这些函数时,您将在最后一行代码中看到:

this.setRoom(this.pointPartOfDay,this.pointPartOfWeek);

Then everything starts to make sense.然后一切都开始变得有意义了。 Before, I just set pointPartOfWeek to its new value and moved on.之前,我只是将 pointPartOfWeek 设置为其新值并继续。 but pointPartOfDay is just an array address.但 pointPartOfDay 只是一个数组地址。 I never actually updated the room cost.我从来没有真正更新过房间费用。 This also explains why cost eventually corrects itself.这也解释了为什么成本最终会自我修正。 As long as pointPartOfWeek doesn't change, calls to setRoom will still fix the amount (even in another event).只要 pointPartOfWeek 不改变,对 setRoom 的调用仍然会固定数量(即使在另一个事件中)。

The fun of debugging...调试的乐趣...

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

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