开发者

Updating a value using jquery

开发者 https://www.devze.com 2023-04-07 10:27 出处:网络
I have created a shortlist system, its like what is used on realestate websites so you can make开发者_如何学Python a list of favorites (using a session arrray) which you can come back to. The system u

I have created a shortlist system, its like what is used on realestate websites so you can make开发者_如何学Python 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?).

0

精彩评论

暂无评论...
验证码 换一张
取 消