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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应