dpfqy5976 2018-02-14 08:48 采纳率: 100%
浏览 181
已采纳

foreach循环中的自定义输入编号

I have make a new style for my input number, and this work well. My problem is when I add this is a foreach structure this doesn't work. I think the problem is the ID change the value, but inside the foreach all all inputs have the same ID.

This is my code inside the foreach:

    <div class='value-button' id='decrease' onclick='decreaseValue()' value='Decrease Value'>-</div>
                <input size='1' id='number' name='product[<?= $p->id ?>]' value='<?= $p->qty ?>' />
<div class='value-button' id='increase' onclick='increaseValue() 'value='Increase Value'>+</div>

This is the js code:

    function increaseValue() {
  var value = parseInt(document.getElementById('number').value, 10);
  value = isNaN(value) ? 0 : value;
  value++;
  document.getElementById('number').value = value;
}

function decreaseValue() {
  var value = parseInt(document.getElementById('number').value, 10);
  value = isNaN(value) ? 0 : value;
  value < 1 ? value = 1 : '';
  value--;
  document.getElementById('number').value = value;
}

I have try to give unique id, but dosen't work. If I add like <?= $p->id ?> instead of id number this does not work.

How I can do? If needed, I can also change the html of the input.

Thank you

This is My last test:

<td class="qty">
      <div class="value-button" id="decrease" onclick="decreaseValue()" value="Decrease Value">-</div>
        <input size="1" id="92" name="product[92]" value="3">
      <div class="value-button" id="increase" onclick="increaseValue() " value="Increase Value">+</div>
      <script>
      function increaseValue() {
        var value = parseInt(document.getElementById('92').value);
        value = isNaN(value) ? 0 : value;
        value++;
        document.getElementById('92').value = value;
      }

      function decreaseValue() {
        var value = parseInt(document.getElementById('92').value);
        value = isNaN(value) ? 0 : value;
        value < 1 ? value = 1 : '';
        value--;
        document.getElementById('92').value = value;
      }
      </script>
    </td>

I try to add the js inside the foreach, with the same id of the input. But doesn't work.

  • 写回答

1条回答 默认 最新

  • dswy34539 2018-02-14 09:01
    关注

    You could use the id of the input as parameter for calling increaseValue and decreaseValue. The attribute id should be unique and you could use a proper prefix and the product number/identifier for it.

    Maybe you change the quoting for attribute to ", instead of ' (this could be a matter of a given style guide). If you do so, then stay allways by the same style. You could nest quotes by using the other unused quote inside of a javascript parameter.

    onclick="decreaseValue('product[<?= $p->id ?>]')"
            ^                                       ^  outer
                           ^                      ^    inner
    

    function increaseValue(id) {
        var value = +document.getElementById(id).value || 0;
        value++;
        document.getElementById(id).value = value;
    }
    
    function decreaseValue(id) {
        var value = +document.getElementById(id).value || 0;
        value--;
        if (value < 1) {
            value = '';
        }
        document.getElementById(id).value = value;
    }
    <div class="value-button" onclick="decreaseValue('product[<?= $p->id ?>]')">-</div>
    <input size="1" id="product[<?= $p->id ?>]" name="product[<?= $p->id ?>]" value="" />
    <div class="value-button" onclick="increaseValue('product[<?= $p->id ?>]')">+</div>

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

报告相同问题?

悬赏问题

  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口