简体   繁体   中英

Updating a value using 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 )

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?).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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