Вы связываете событие щелчка с каждым экземпляром incrementer_minus
и incrementer_plus
, когда вы делаете:
$(".incrementer_plus").live('click'...
Вместо этого вам нужно просто прикрепить событие к определенномутот, который был только что создан.
Я изменил ваш код, чтобы сделать это, и я также использовал bind
вместо live
, так как вам просто не нужно live
.
Я также изменил JQ
на jQuery
для моего удобства.Вы можете изменить его обратно.
Проверьте это: http://jsfiddle.net/mAsr9/
jQuery.fn.incrementer = function() {
var myobj = this;
this.val(0);
// Create new element, and insert before 'this'
jQuery("<input class='incrementer_minus' type='button' value=' - '/>").insertBefore(this)
// Then bind the click event to that new element
.bind('click', function(){
$(myobj).val(parseInt(jQuery(myobj).val()) - 1);
});
// Create new element, and insert after 'this'
jQuery("<input class='incrementer_plus' type='button' value=' + '/>").insertAfter(this)
// Then bind the click event to that new element
.bind('click', function(){
$(myobj).val(parseInt(jQuery(myobj).val()) + 1);
});
}
$(function() {
jQuery("#increment").incrementer();
jQuery("#increment2").incrementer();
});