简体   繁体   English

Javascript / Jquery:将新文本分配给span对象时的奇怪行为

[英]Javascript/Jquery: Odd behavior when assigning new text to span object

This is driving me insane. 这让我发疯。 This is part of a Laravel blade template but I'm not sure that's relevant. 这是Laravel刀片模板的一部分,但我不确定是否相关。

Context 上下文

I have javascript code that is variable and server-side rendered based on the number of attendees for an event. 我有可变的javascript代码,并根据事件的参加者数量在服务器端进行渲染。 The number of repeated fields, with pricing information, etc. is repeated n times. 具有价格信息等的重复字段数被重复n次。

I have a couple of dynamic updates that can occur based on user input which attempt to change 2 span objects: 我有一些基于用户输入的动态更新,尝试更改2个span对象:

  1. Default Ticket Price (span ids: $("#tcost"+n) where n is a number from 1-5) 默认机票价格( $("#tcost"+n) ID: $("#tcost"+n) ,其中n是1-5中的数字)
  2. Subtotal (taking any discounts into account) (span ids: $("#final"+n) 小计(考虑所有折扣)(跨度ID: $("#final"+n)

Odd Behavior 奇怪的行为

I've verified that I can access the text/html contained within both spans with: 我已验证可以使用以下命令访问两个跨度中包含的文本/ html:

    tc = document.getElementById('tcost'+n)
    fc = document.getElementById('final'+n)

When I try to change the values I've attempted both: 当我尝试更改值时,我同时尝试了两种方法:

    tc.innerHTML = 'whatever';  // setting fc.innerHTML didn't change it
    $('#final'+n).text('whatever');
    blah = $('#final'+n).text();

In both instances, the tc value changes just fine. 在这两种情况下, tc值变化都很好。 But when I try to change the value in "final"+n using either format, it does not change on the screen. 但是,当我尝试使用任一格式更改"final"+n的值时,它在屏幕上都不会更改。

HOWEVER , when I console.log(blah) I see the correct value. 但是 ,当我console.log(blah)我看到了正确的值。

Is there something esoteric I'm missing as to why the either of the above approaches to setting the text of the span to something would register as set but not display? 关于上述任何一种将跨度文本设置为某些内容的方法为何会注册为设置但不显示的问题,我是否缺少一些深奥的东西?

In the process of making the minimal, complete, verifiable example, I discovered the issue. 在制作最小,完整,可验证的示例的过程中,我发现了问题。 Apologies. 道歉。

And the answer was I had a function that was changing the value after the fact back to the value so it was appearing to not change. 答案是我有一个函数,可以在事实恢复为原先的值后将其更改为原来的值,因此它似乎没有变化。 Ugh. 啊。

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

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