0

The following jQuery works fine to add rows, and populate the select list from a php/mysql file. The issue I am having is that the price field is being auto-populated and changed on select change on each new row that is added... but if you go back and try to change one of the rows that has already been added, it does not work. For instance, if you add 4 blank rows and then try to go back and select products, it will only populate the price on the last row. Or if you add each row and select the products before you add a new row, then go back and attempt to change the product, the price previously selected will remain the same. Any ideas?

EDIT 1 Added html

EDIT 2 Changed jQuery change(function() to on()... still works exactly the same. The autocomplete only works on the last row added.

var count = 0;

$(document).ready(function(){

$('p#add_field').click(function(){

    count += 1;

    $('#addingContainer').append('<label>Product or Service</label><select id="product_' + count + '" name="products[]'+ '" ><option value=""></option>');

    $.getJSON('includes/productrow.php', function(data){
            var select = $('#product_' + count + '');
            $.each(data, function(key, val) {
                $('<option/>').attr('value', val.product_id)
                              .attr('data-price', val.price)
                              .html(val.product)
                              .appendTo(select);
    });

    $('#addingContainer').append('</select>&nbsp;&nbsp;<label>Price</label><input type="text" id="price_' + count + '" name="prices[]' + '" class="price" ><br>');
    });

    $('#addingContainer').on('change', 'select[id="product_' + count + '"]', function(){

        $('#price_' + count +'').val($('select[id="product_' + count + '"] option:selected').data('price'));

});});});

HTML:

<div id="addingContainer" class="pure-control-group">
         <p id="add_field"><a href="#"><span>Add Products</span></a></p>
         </div>
1
  • since you are adding new fields dynamically you need to edit your select change function to use jquery's on() function api.jquery.com/on Commented Sep 16, 2014 at 15:10

2 Answers 2

1

edit Actually, I think the reason it isn't working is because of where you declared count. Something like this would work:

$(document).ready(function(){

$('p#add_field').click(function(){
    var count = $('#addingContainer').data("count") || 0;
    count += 1;

    $('#addingContainer').data("count", count).append('<label>Product or Service</label><select id="product_' + count + '" name="products[]'+ '" ><option value=""></option>');

    $.getJSON('includes/productrow.php', function(data){
            var select = $('#product_' + count + '');
            $.each(data, function(key, val) {
                $('<option/>').attr('value', val.product_id)
                              .attr('data-price', val.price)
                              .html(val.product)
                              .appendTo(select);
    });

    $('#addingContainer').append('</select>&nbsp;&nbsp;<label>Price</label><input type="text" id="price_' + count + '" name="prices[]' + '" class="price" ><br>');
    });

    $('#addingContainer').on('change', 'select[id="product_' + count + '"]', function(){

        $('#price_' + count +'').val($('select[id="product_' + count + '"] option:selected').data('price'));

});});});

Note I removed your initial declaration of count outside of the document.ready and the function closure; that allowed you to keep track of it (I handled that by setting it on a data attribute, there are other and better ways) but that was why your on('change' method was always using the highest count.

By declaring count inside the function, it is specific to that closure -- and so it won't increase for all of the functions you declared each time a new click event is fired.

Sign up to request clarification or add additional context in comments.

Comments

0

Give this a try. I added a class "pselect" to the select box which makes it easier to work with. Then I modified the change function.

var count = 0;

$(document).ready(function(){

$('p#add_field').click(function(){

count += 1;

$('#addingContainer').append('<label>Product or Service</label><select id="product_' + count + '"    name="products[]'+ '" class="pselect"><option value=""></option>');

$.getJSON('includes/productrow.php', function(data){
        var select = $('#product_' + count + '');
        $.each(data, function(key, val) {
            $('<option/>').attr('value', val.product_id)
                          .attr('data-price', val.price)
                          .html(val.product)
                          .appendTo(select);
});

$('#addingContainer').append('</select>&nbsp;&nbsp;<label>Price</label><input type="text" id="price_' + count + '" name="prices[]' + '" class="price" ><br>');
});

$('#addingContainer').on("change",".pselect",function(){
$(this).next(".price").val($("option:selected", this).attr('data-price'));
});
});});

5 Comments

Thanks for the input! That didn't work though :( Nothing populates in the text field at all.
can you post your html?
Just added. There's not much because everything is being added through the jquery.
i noticed that its not "data('price')" that holds the price info in the option element. it's "attr('data-price')"
it is populating fine though. As long as they are added, and then populated individually the price is being populated correctly. It just doesn't change the price field value if you go back and try to change the product once another row has already been added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.