[Fixed]-How to connect button and field value with jQuery for object in for loop from template

1đź‘Ť

âś…

You can either do:

 var quantity = $(this).parents('form').find('#quantity').val();

or more efficiently you can select the form itself. Ex:

 $('form').on('submit', function(){
    var link = $(this);
    var quantity = $(this).find('#quantity').val();
    $.ajax({
        'url': link.attr('action'),
        'type': 'POST',
        'dataType': 'json',
        'data': {
            'quantity': quantity,
            'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val()
        },
        'success': function(data, status, xhr){
            alert(data['status']);
            return false;
        }
    });
    return false;
});

Your issue is you’re saying “select the first “#quantity” on the page and get its value. Where you want to get the targeted forms #quantity’s value.

Leave a comment