dsy1971 2017-01-12 08:30
浏览 286

复选框Javascript禁用按钮

I have an array of users in php that i send to the view (in laravel) and do a foreach to list all users in table. For now it is ok. I have a "send" button that appear disable but i want to put visible when i click on the checkbox.

enter image description here

I put this javascript code:

<script type="text/javascript">
Enable = function(val)
{
    var sbmt = document.getElementById("send"); //id of button

    if (val.checked == true)
    {
        sbmt.disabled = false;
    }
    else
    {
        sbmt.disabled = true;
    }
}    
</script>

and call the function onClick method of checkbox:

onclick="Enable(this)"

But the problem is when i click on the check box only the first button send works and appear visible. If i click for example in check box of user in position 2,3,4...etc the buttons send of these users stay disabled, only the first appear visible. This code only work to the first position of send button.

I appreciate your help :)

Regards

  • 写回答

2条回答 默认 最新

  • douyinghuo8874 2017-01-12 09:23
    关注
    1. You pass element to function Enable, but treat it as value. You should extract value from element before other actions.
    2. You hardcoded button id in the function. It shout be passed outside and should differs for each button.

    Enable = function(checkbox, btnId)
        {
            document.getElementById(btnId).disabled = !checkbox.checked; // ← !
        }
    <button id="send1" disabled>SEND</button>
    <input type="checkbox" onclick="Enable(this, 'send1')"><br>
    <button id="send2" disabled>SEND</button>
    <input type="checkbox" onclick="Enable(this, 'send2')"><br>
    <button id="send3" disabled>SEND</button>
    <input type="checkbox" onclick="Enable(this, 'send3')">

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?