dqmnueuu459279 2015-06-07 12:47
浏览 49
已采纳

jQuery从php生成的输入文本中选择输入值

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();
        }
    });
});
  • 写回答

1条回答 默认 最新

  • douchao1957 2015-06-07 13:34
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了