首页 > 解决方案 > 如何从所有 php 文本 id 中获取元素?

问题描述

php+js。我想getElementById所有文本id,但结果只有我能得到最后一个id。我不知道该怎么处理它...

html:

<div class="col-md-3 py-5">
    <button type="button" class="btn bg-light border rounded-circle" onclick="minus()"><i class="fas fa fa-minus" ></i></button>
    <input type="text" id="<?php echo $pro_id?>" value="<?php echo $pro_quantity?>" class="form-control w-25 d-inline">
    <button type="button" class="btn bg-light border rounded-circle" onclick="plus()"> <i class="fas fa fa-plus" ></i></button>
</div>

js:

<script>
    let count = 1;
    let countEl = document.getElementById("<?php echo $pro_id;?>");
    function plus(){
        count++;
        countEl.value = count;
    }
    function minus(){
        if (count > 1) count--;
        countEl.value = count;s
    }
</script>

铬 js 照片:

Chrome js照片

标签: javascriptphphtmlgetelementbyid

解决方案


我们只能为一页使用一个 id。

使用类而不是 id。

或者如果可能的话,在 plus(id) 函数和 minus(id) 函数中传递您当前的 id


推荐阅读