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

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 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?