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>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致