简体   繁体   English

如何将变量与Jquery结合?

[英]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 我想做的是创建一个变量并将其递增,然后将其添加到上面的代码中,以替换.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);
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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