douhui1630 2019-08-06 17:25
浏览 59

如何从具有相同类名的输入元素中获取特定输入值[关闭]

Basically I'm running a php while loop which returns a container for for every row the statement is true.

The container contains input fields, and as you can imagine they all have the same class names in all the containing divs

On user keyup/input I want to get the that specific elements value and parent container using JavaScript/ JQuery

My JavaScript looks like this:

     $(document).ready(function() {
    function delay(fn, ms) {
        let timer = 0;
        return function(...args) {
            clearTimeout(timer)
            timer = setTimeout(fn.bind(this, ...args), ms || 0)
        }
    }
   $(".quantity").on('keyup input',delay(function(e) {
    console.log('Time elapsed!', this.value);
  var a = parseInt(document.getElementById('quantity').value);
  var b = parseInt(document.getElementById('price').value);
  var Qprice = a * b;
    $.ajax({
        url: 'cartFx.php',
        type: 'post',
        data: { Qprice: Qprice},
        success: function(response) {
            document.getElementById('Qprice').innerHTML = Qprice;
            alert(Qprice);
        }
});
},500));
});

The PHP code

$i = 0;
                while($i < mysqli_num_rows($cart_products)) {
                    $meta = mysqli_fetch_assoc($cart_products);
                    $Pname = $meta['Pname'];
                    $inCartId = $meta['id'];
                    $price = $meta['price'];
                    $Bname = $meta['FromBusiness'];
                    $image = $meta['image'];
                    $code = $meta['code'];

?>
        <div class='cart_body'>
            <input id ='price' name='price' value='<?php echo $price?>'></input>
            <input class="quantity" id ='quantity' type="number" name='quantity' value='<?php 
             echo $quantity?>'></input>
             <p id="Qprice"></p>

        </div>
<?php           
$i= $i + 1;
                }

    }else
    {
?>
        ....
<?php
    }
}
?>
  • 写回答

3条回答 默认 最新

  • douxian4376 2019-08-06 18:36
    关注

    In your js, you have this $(".quantity").on('keyup input',delay(function(e) { which says that there is an element with class quantity. Yet, in your php code, quantity is an id and not a class. Not sure how it is working on the first element but change that id="quantity" to class="quantity" and it should work

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本