开发者

Jquery - Every click adds another integer

I have been searching for an answer for this for days and haven't found anything so sorry if this has been asked before.

I want the integer to开发者_JAVA百科 go up by 1 every time this link is clicked. So first it is 0, then clicked its 1, then clicked its 2, then clicked its 3

document.getElementById('myPackage').href="http://www.vanquishvps.com/order/cart.php?a=confproduct&i="+1

I really have no idea how to do this, I have really tried looking at Jquerys variables and stuff, I just cant figure it out.

Id appreciate any help hugely!

Thank you


After looking at the site that you mentioned in your comment. This should work in your specific scenario:

jQuery('#myPackage').click(function () {
  // Get the first 3 parts of link's href
  var hrefParts = jQuery('#myPackage')[0].href.split('=').slice(0,2);

  // Add the current slots value
  hrefParts.push(jQuery('.slots span').text())

  // Set the href to the new URL
  this.href = hrefParts.join('=');
});

Another solution would be to modify your existing setPackageData function and add these lines to the end of it. This solution would set the link to the package index, instead of the number of slots, and also does it whenever you change the slider, not just when you click the link:

var myPackage = $('#myPackage')[0], hrefParts = myPackage.href.split('=').slice(0,2);
hrefParts.push(index.toString());
myPackage.href = hrefParts.join('=');

Although you should have your developer look at it, she or he will probably come up with a more permanent solution ;)


You need to attach a click event handler:

Vanilla Javascript:

 document.getElementById('myPackage').attachEvent('click', function()
 {
      // If it's some script value;
      someValue++;

      // If it's the text of your link...
      var currentValue = parseInt(this.value);
      this.value = currentValue++;

      return false;
 });

jQuery:

$('#myPackage').click(function()
{
     someValue++;
     $(this).val(parseInt(this.value)++);
});


You need to have a global variable in javascript to have the id and then bind the click event.

var myID = 0;

$("#myPackage").click(function() {
      myID++;
      window.location = "http://www.vanquishvps.com/order/cart.php?a=confproduct&i=" + myID;
});


store your number variable somewhere else.

var index = 0;

and then use it to change your link's url

function changeUrl() {

index++;

document.getElementById('myPackage').href="http://www.vanquishvps.com/order/cart.php?a=confproduct&i=" + index;

}

so, when the user clicks wherever you want him to click to change the url, you just have to call the changeUrl function

document.getElementById('elementThatWillChangeTheUrl').click = changeUrl;

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜