简体   繁体   中英

How to combine variables with Jquery?

Got the following code:

$(document).ready(function() 
{
   $('a.add-item').click(function()
   {
         if ($(this).parent().find('input').attr('value', '0'))
         {
            $(this).parent().find('input').attr('value', '1')
         }
   });
});

What I would like to do is to create a variable and increment it and then add it to my above code replacing .attr('value', '0')) 0

How would I go about this?

thanks, Keith

You mean like this:

$(document).ready(function() 
{
   $('a.add-item').click(function()
   {
         var $item = $(this).parent().find('input');
         var value = parseInt($item.attr('value'), 10);
         if (!value) {
             value = 0;
         }
         $item.attr('value', value + 1);
   });
});

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