weixin_33713503 2011-09-24 09:35 采纳率: 0%
浏览 21

使用jQuery更新值

I have created a shortlist system, its like what is used on realestate websites so you can make a list of favorites (using a session arrray) which you can come back to. The system uses jquery to update the session array with a new shortlist item.

What I am not sure how to do is update the 'shortlist/ array count' using jquery. I know how to change the HTML of an id, but how could I retrieve the session count and + 1 when something has been added a new item to the array. Here is how I display my 'shortlist count':

Shortlist: " (view)
  • 写回答

1条回答 默认 最新

  • weixin_33725126 2011-09-24 10:22
    关注

    Until you post code of element containing the html of the part of page you want to update, no one will be able to help you much, but lets say that you have something like this:

    <div id="shortlist">
        You have <span id="itemCount">2</span> items in your cart.
    </div>
    

    You can update value from jQuery like this:

    var oldValue = $("div#shortlist").find("span#itemCount").text();
    var newValue = parseInt(oldValue) + 1;
    $("div#shortlist").find("span#itemCount").text(newValue);
    

    Updating your html, which looks like:

    <div id="shortlist">3 (<a href="/shortlist">view</a>) </div> 
    

    would be much simpler if you would change php to something like:

    <div id="shortlist"><span id="itemCount"><? echo count($_SESSION['shortlistArray']); ?></span>" (<a href="/shortlist">view</a>) </div>
    

    It is good to have value to update in html element you can identify through jQuery. If this is not option, then you need to update value using string.replace (probably searching for regex if you don't now old value?).

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题