使用javascript获取Yii中所选复选框的所有ID

时间:2014-04-09 06:13:36

标签: javascript php jquery yii

我正在尝试使用JAVASCRIPT获取yii中所有选中复选框的ID。现在我只能获得第一个元素ID。任何人都可以建议正确的代码来获取所有复选框ID。

我的观点:

<input type="button" value="Multiple Host Date Entries" onclick="act();" />

 <div id="grid"></div>
<?php 
//zii.widgets.grid.CGridView  bootstrap.widgets.TbExtendedGridView
$obj=$this->widget('bootstrap.widgets.TbExtendedGridView', array(
    'id'=>'host_grid',
    'dataProvider'=>$dataProvider,
    'type' => 'striped bordered',

    //'filter'          => $model,
     //'type' => 'striped bordered condensed',
               //'summaryText' => false,
     ////'afterAjaxUpdate'=>'\'changeTRColor()\'',
    //'itemView'=>'_view',
    'columns'=>array(
                                array(
                                'id' => 'selectedIds',
                                'class' => 'CCheckBoxColumn',
                                    'selectableRows'=>2,
                                    'value'          => '$data->host_id',
'checkBoxHtmlOptions' => array('name' => 'idList[]'),
                            ),

                array(            // display 'create_time' using an expression
                            'name'=>'host_name',
                                    'value'=>'$data->host_name',
                ),
                array(
                            'name'=>'host_serviceid',
                            'value'=>'$data->host_serviceid',
                ),
                                array(
                            'name'=>'status',
                            'value'=>'$data->status',
                ),
                array(
                                    'class'=>'CButtonColumn',
                                    'template'=>'{edit_date}{update}{delete}',
                                    'htmlOptions'=>array('width'=>'95px'),
                                    'buttons' => array(

                                        'update'=> array(
                                            'label' => 'Update',
                                            'imageUrl' => Yii::app()->baseUrl.'/images/icons/a.png',
                                            ),

                                        'delete'=> array(
                                            'label' => 'Delete',
                                            'imageUrl' => Yii::app()->baseUrl.'/images/icons/d.png',
                                            ),

                                        'edit_date' => array( //the name {reply} must be same
                                             'label' => 'Add Date', // text label of the button
                                               'url' => 'Yii::app()->createAbsoluteUrl("NimsoftHostsDetails/View", array("id"=>$data->host_id))', //Your URL According to your wish
                                                  'imageUrl' => Yii::app()->baseUrl.'/images/icons/m.png',  // image URL of the button. If not set or false, a text link is used, The image must be 16X16 pixels

                                               ),
                                           ),)

                ),

))
        ; 

?>

我必须选中一些复选框并单击多个主机日期条目按钮才能转到特定控制器。

我的JavaScript:

function act()
{
        var idList=$("input[type=checkbox]:checked").serializeArray();
        var jsonStr = JSON.stringify(idList);

       /* Object.keys(idList).forEach(function(key) {
    console.log(key, idList[key]);
    alert(idList[key]);
});*/
        var a=$("input[type=checkbox]:checked").val();
        alert(a); 
        if(idList!="")
              {
                  if(confirm("Add Dates for multiple hosts?"))
                  {
                      var url='<?php echo $this->createUrl('Nimsoft/Date_all',array('idList'=>'val_idList')); ?>';
                      url=url.replace('val_idList',jsonStr);
                      //url=url.replace('val_idList',json_encode(idList));
                      //alert(url);
                      window.open(url);

                      /*$.post('Date_all',idList,function(response)
                      {
                          $.fn.yiiGridView.update("host_grid");
                      });*/
                  }
              }
        else
        {
            alert("Please Select atleast one host");
        }

}

我需要将ID传递给NIMSOFT控制器,以便我可以使用for循环来处理每一个。

2 个答案:

答案 0 :(得分:1)

对于所有已检查的行ID,我们使用此

var id = $.fn.yiiGridView.getChecked("your-grid-id", "selectedIds"); // array of seleted id's from grid

答案 1 :(得分:0)

您可以检索checked column in client side(javascript):