开发者

Jquery .next() function not working

开发者 https://www.devze.com 2023-01-31 10:14 出处:网络
Guys i am trying to do something like this i have two href and a textbox in the middle of those <-> TEXT <+>

Guys i am trying to do something like this i have two href and a text box in the middle of those <-> TEXT <+> So when i press the - and + the value in the txt must increase or decrease by one

<!-- <div class="quantity noprint">
   <div  class="subtract" title="Less Quantity"></div><input type="text" id="addToCartQty<%=addProduct.getLongID()%>" value="<%=addProduct.getInteger("ATR_WebMinQuantity",1)/addProduct.getInteger(MCRConstants.DM_ATR_LEGACY_CASE_VENDOR_PACK_SIZE,1) %>" name="ADD_CART_ITEM<>quantity" class="text" maxlength="3" /><div  class="add" title="Add Quantity"></div>

</div> --!>

and i am using a jquery to + and - the value in the text box. Whenever i press + its happening correctly but for - it takes the TEXT fields name instead of its value . Any solution for this to make it to take the value of the TEXT box Jquery used follows :

 $(".quantity .subtract").click(function () {
        var qtyInput = $(this).next('input');
        var qty = parseInt(qtyInput.val());
        if (qty > 1)
            qtyInput.val(qty - 1);

        qtyInput.focus();
        return false;
    });

    $(".quantity .add").click(function () {
        var qtyInput = $(this).prev('input');
        var qty = parseInt(qtyInput.val());
        if (qty >= 0 && (qty + 开发者_StackOverflow中文版1 <= 999))
            qtyInput.val(qty + 1);

        qtyInput.focus();
        return false;
    });


.next() returns the next sibling of each selected element. If a selector is passed into the function, the next sibling is returned only if it matches the selector. Since your + and - links are separate elements, they will have different next siblings. You are looking for .siblings():

var qtyInput = $(this).siblings('input');


You're probably looking for the nextAll() function: http://api.jquery.com/nextAll/.


Maybe you can try another type of query:

$(".quantity .subtract").click(function () {
    var qtyInput = $(this).closest('.quantity').find('input');
    var qty = parseInt(qtyInput.val());
    if (qty > 1)
        qtyInput.val(qty - 1);

    qtyInput.focus();
    return false;
});

$(".quantity .add").click(function () {
    var qtyInput = $(this).closest('.quantity').find('input');
    var qty = parseInt(qtyInput.val());
    if (qty >= 0 && (qty + 1 <= 999))
        qtyInput.val(qty + 1);

    qtyInput.focus();
    return false;
});


this works. I cleaned up some of unnecessary for this sample properties.

UPDATE:

Could ADD_CART_ITEM<>quantity be a reason for an issue? > can be interpreted as a closing tag and result in a strange behavior. At least, as you can see from my sample, your code should work, unless there are some hidden issues in it. The idea is definitely correct.


Finally i solved it, Find the jquery below

  $(".quantity .subtract").click(function () {
        var qtyInput = $(this).nextAll('a');
        var qtytxt = $(qtyInput).prev('input');
        var qty = qtytxt.val();

        if (qty > 1)
            qtytxt.val(qty - 1);
        qtytxt.focus();
        return false;
    });

I simply got all next for the href and got the prev one , hence got the value of the TEXT as lik how i wanted. Thanks to all u guys , Cheers ;)

0

精彩评论

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