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 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64