dongxiaoguang9108 2013-07-17 14:08
浏览 75
已采纳

将单个复选框值指定为数组

Is there a way of assigning the value of a checkbox as an array.

I have a load of checkboxes, one of which is an all option. I would like to set the value of this to an array?

I have tried creating an array of ints (ids) from an array of my objects using a simple loop and then using print_r in the value (I know this is a bit ugly and I can see why this wouldn't work but can't seem to find the correct syntax).

$arrAllID = array();
foreach ($AvailableGroups as $objASRDCallBackGroup)
{
    $arrAllID[] = $objASRDCallBackGroup->m_iGroupID;
}

<input id="group-name" value="<?php print_r($arrAllID) ?>" name="SelectedGroups[]" type="checkbox">All

Also I could design around this and do the retrieval of the collection when the form is posted by checking if all was selected or something but I would really like to know how you can do this now..

Any help greatly received.

Thanks

  • 写回答

1条回答 默认 最新

  • duanfu5239 2013-07-17 14:16
    关注

    You can use the HTML5 data attribute to store your array of values. I'm not very familiar with PHP, but the data attribute will render on the client, and if this checkbox is clicked, you can loop through the array in the data attribute and do whatever you need to send data back to the server.

    <input id="group-name" data-allid="<?php print_r($arrAllID) ?>" name="SelectedGroups[]" type="checkbox">All
    
    function someClickHandler() {
         var chkGroupName = document.getElementById('group-name');
         var allIds = chkGroupName.attr('data-allid');
        // do what you need with the array of ids.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作