输入值和div的innerHTML随Javascript改变,但在显示中不改变

发布于 2025-01-08 00:55:23 字数 2574 浏览 0 评论 0原文

我有一个 INPUT 文本字段、一个 DIV 和一个 IMG。

IMG 有一个 onClick 事件:

  • 读取 INPUT 字段的当前值,

  • 将其增加 1,

  • 执行使用增加的值进行计算,

  • 将结果写入 DIV。

INPUT 的值始终以正确的方式增加,DIV 的innerHTML 始终获得正确的结果但显示没有任何变化。即使一切在“后台”中正确完成,显示的数字也始终保持不变。

有趣的是,我在同一网站的另一个地方使用了相同的操作,一切都正常运行并完美显示

这是该函数:

    function priceCalculator(max_amound,price,id)
    {
        var amound = parseInt(document.getElementById('sell_amound_' + id).value);
        max_amound = parseInt(max_amound);
        if (amound < max_amound)
        {
            amound = amound + 1;
            document.getElementById('sell_amound_' + id).value = amound;
            var item_value = amound * price;
            document.getElementById('price_' + id).innerHTML = item_value;

            alert(document.getElementById('sell_amound_' + id).value + ',' + document.getElementById('price_' + id).innerHTML);
        }
    }

以下是 PHP 代码中的元素:

<img src="images/plus.png" onclick="priceCalculator(\''.$bag_items[$i]['amound'].'\',\''.$bag_items[$i]['infos']['price'].'\',\''.$i.'\')" />
<form>
    <input id="sell_amound_'.$i.'" type="text" readonly value="1" />
</form>
<div id="price_'.$i.'">'.$bag_items[$i]['infos']['price'].'</div>

函数末尾的警报显示正确的值,但显示的值保持不变。

这是一个非常简单的操作...可能有什么问题?

编辑:

加载“item”的源代码后看起来像这样(这些部分是使用数据库中的循环创建的,当然,我从代码中删除了不相关的样式,并且那些许多div是因为它们):

<td>
    <img id="item_pic_3" src="images/potions/3.png" onClick="shopSellInfo('3')" />
    <div>26</div>
    <form>
        <input type="hidden" id="selected_item" value="" />
    </form>
    <div id="item_3" style="display: none;">
        <span>blah...</span><br />
        <span>
            <br />blah...<br /><br />
            <div>
                <div>
                    <img src="images/increase.png" onclick="priceCalculator('26','10','3')" /><br />
                </div>
            <div>
            <form>
                <input id="sell_amound_3" type="text" readonly value="1" />
            </form>
            /26
            </div>
        </div>
        <br />
        <div id="price_3">10</div>
        </span>
    </div>
</td>

shopSellInfo('3') 是使 item_3 显示在正确位置的函数。

I have an INPUT text field, a DIV and an IMG.

The IMG has an onClick event:

  • reads the INPUT field's current value,

  • increase it with 1,

  • does a calculation with the increased value,

  • writes the result into the DIV.

The value of the INPUT is always increased the right way and the DIV's innerHTML always gets the right result but nothing changes in display. The displayed numbers always stay the same even if everything is done correctly in the "background".

The funny thing in it that I used the same operation at another place on the same site and there everything works and displays perfectly.

Here is the function:

    function priceCalculator(max_amound,price,id)
    {
        var amound = parseInt(document.getElementById('sell_amound_' + id).value);
        max_amound = parseInt(max_amound);
        if (amound < max_amound)
        {
            amound = amound + 1;
            document.getElementById('sell_amound_' + id).value = amound;
            var item_value = amound * price;
            document.getElementById('price_' + id).innerHTML = item_value;

            alert(document.getElementById('sell_amound_' + id).value + ',' + document.getElementById('price_' + id).innerHTML);
        }
    }

And here are the elements within a PHP code:

<img src="images/plus.png" onclick="priceCalculator(\''.$bag_items[$i]['amound'].'\',\''.$bag_items[$i]['infos']['price'].'\',\''.$i.'\')" />
<form>
    <input id="sell_amound_'.$i.'" type="text" readonly value="1" />
</form>
<div id="price_'.$i.'">'.$bag_items[$i]['infos']['price'].'</div>

The alert at the end of the function shows the right values but the displayed values stay the same.

It's a really simple action... What could be the problem?

EDIT:

After loading the source code of an "item" looks like this (these parts are created with loops from database, and, of course, I removed the irrelevant styling from the code and those many divs are there because of them):

<td>
    <img id="item_pic_3" src="images/potions/3.png" onClick="shopSellInfo('3')" />
    <div>26</div>
    <form>
        <input type="hidden" id="selected_item" value="" />
    </form>
    <div id="item_3" style="display: none;">
        <span>blah...</span><br />
        <span>
            <br />blah...<br /><br />
            <div>
                <div>
                    <img src="images/increase.png" onclick="priceCalculator('26','10','3')" /><br />
                </div>
            <div>
            <form>
                <input id="sell_amound_3" type="text" readonly value="1" />
            </form>
            /26
            </div>
        </div>
        <br />
        <div id="price_3">10</div>
        </span>
    </div>
</td>

shopSellInfo('3') is the function that makes item_3 displayed at the right place.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2025-01-15 00:55:23

您能否查看源代码并显示输出内容:

<div id="price_'.$i.'">'.$bag_items[$i]['infos']['price'].'</div>

检查 div id 是否与 JavaScript 相对应。

编辑item_3具有CSS样式display:none,这就是更改未显示的原因。

Can you please view source and show what is output by:

<div id="price_'.$i.'">'.$bag_items[$i]['infos']['price'].'</div>

Check that the div id is corresponding to the JavaScript's.

EDIT: item_3 has CSS style display: none, that is why changes are not showing up.

我偏爱纯白色 2025-01-15 00:55:23

我解决了问题!

问题是 ID 也出现在其他地方,因此函数不知道在哪里更改值,因为 ID 重复。

无论如何,感谢您的回答并寻求帮助!

I solved the problem!

The problem was that the IDs appeared at another place, too, so the function didn't know where to change the values because of the duplicated IDs.

Anyway, thanks your answers and will for help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文