0

I have this structure that I generate for each element find by a search

<?php foreach ($this->pneumatici as $pneumatico) :?>
<div class="col-md-3 element-form">
    <div class="input-group input-float">
        <input type="hidden" name="codice" value="<?= $pneumatico->codice ?>" id="codice">
        <input type="text" class="form-control" id="quantita" value="0" size="1" maxlength="2"
         <?php if ($pneumatico->quantita == 0){
             echo "disabled"
         } ?> >
    </div>
    <?php if ($pneumatico->quantita == 0){
        echo '<button class="btn btn-danger btn-disabled" role="button"><strong>ESAURITO</strong></button>';
    } else {
        echo '<button class="btn btn-primary insert-cart">Aggiungi <span class="glyphicon glyphicon-shopping-cart"></span></button>';
    } ?>
    <div class="clearfix"></div>

I need when user click on one of the generate button with class='insert-cart' to put in array these attributes:

  • '#quantita' => val,
  • '#codice' => val

How I can do?

I tried in this way, but I can get only the first element value and doesn't work for the others

$(".insert-cart").click(function(){
    var quantTot = "";
    $('#quantita').each(function(){
        var quantita = $('#quantita').val();
        quantTot += quantita;
        if (quantita != 0) {
            var id = $('#codice').val();
        }
    });
});
5
  • Do you have more than one #quanita on the page? IDs should be unique. Try changing to class selector Commented Jun 7, 2015 at 12:53
  • 1
    You're making at least three mistakes. The first is that elements should not share the same ids - use classes or data properties for that. The second is that you want to access not $('#quantita').val(), but rather $(this).val(). The third is that you should probably convert the string value to a float or an int as well. Commented Jun 7, 2015 at 12:54
  • 2
    You are great! I added class to input and get value by that, I used $(this).val() and I converted to int the string, now it works great. thanks Commented Jun 7, 2015 at 13:03
  • @JoelHinz Please answer the question, doing that the users stop coming here to provide some help to unanswered question. Commented Jun 7, 2015 at 13:32
  • 1
    @lmgonzalves I didn't think it was good enough to be used as an answer. But I'll convert it to one since it solved the problem. Commented Jun 7, 2015 at 13:34

1 Answer 1

1

Converting my comment into an answer.

You're making at least three mistakes. The first is that elements should not share the same ids - use classes or data properties for that. The second is that you want to access not $('#quantita').val(), but rather $(this).val(). The third is that you should probably convert the string value to a float or an int as well.

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

Comments

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.