手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

Yii : using multiple radio button

首页 > PHP Framework >

Yii在使用CHtmlRadio的时候,如果参数不正确,会隐藏一个form,导致如果radio的值有3个时,永远只能提交第一个和最后一个
太纠结了。仔细看看,确实 是有一个隐藏FORM,导致本来应该是3个radio的button,变成了6个。

找了一下资料,看了一下源码,果然。。。。有一个hidden。

PHP代码
  1. /** 
  2.  * Generates a radio button for a model attribute. 
  3.  * If the attribute has input error, the input field's CSS class will 
  4.  * be appended with {@link errorCss}. 
  5.  * @param CModel $model the data model 
  6.  * @param string $attribute the attribute 
  7.  * @param array $htmlOptions additional HTML attributes. Besides normal HTML attributes, a few special 
  8.  * attributes are also recognized (see {@link clientChange} and {@link tag} for more details.) 
  9.  * A special option named 'uncheckValue' is available that can be used to specify 
  10.  * the value returned when the radio button is not checked. By default, this value is '0'. 
  11.  * Internally, a hidden field is rendered so that when the radio button is not checked, 
  12.  * we can still obtain the posted uncheck value. 
  13.  * If 'uncheckValue' is set as NULL, the hidden field will not be rendered. 
  14.  * @return string the generated radio button 
  15.  * @see clientChange 
  16.  * @see activeInputField 
  17.  */  
  18. public static function activeRadioButton($model,$attribute,$htmlOptions=array())  
  19. {  
  20.     self::resolveNameID($model,$attribute,$htmlOptions);  
  21.     if(!isset($htmlOptions['value'])) 
  22.         $htmlOptions['value']=1; 
  23.     if(!isset($htmlOptions['checked']) && self::resolveValue($model,$attribute)==$htmlOptions['value']) 
  24.         $htmlOptions['checked']='checked'; 
  25.     self::clientChange('click',$htmlOptions); 
  26.  
  27.     if(array_key_exists('uncheckValue',$htmlOptions)) 
  28.     { 
  29.         $uncheck=$htmlOptions['uncheckValue']; 
  30.         unset($htmlOptions['uncheckValue']); 
  31.     } 
  32.     else 
  33.         $uncheck='0'; 
  34.  
  35.     $hiddenOptions=isset($htmlOptions['id']) ? array('id'=>self::ID_PREFIX.$htmlOptions['id']) : array('id'=>false); 
  36.     $hidden=$uncheck!==null ? self::hiddenField($htmlOptions['name'],$uncheck,$hiddenOptions) : ''; 
  37.  
  38.     // add a hidden field so that if the radio button is not selected, it still submits a value 
  39.     return $hidden . self::activeInputField('radio',$model,$attribute,$htmlOptions);  
  40. }  


太纠结了。居然用unCheckValue设置一下才OK:

PHP代码
  1. echo CHtml::radioButton('btn', false, array(  
  2.     'value'=>'1',  
  3.     'name'=>'btnname',  
  4.     'uncheckValue'=>null  
  5. ));   
  6. CHtml::radioButton('btn', false, array(  
  7.     'value'=>'2',  
  8.     'name'=>'btnname',  
  9.     'uncheckValue'=>null  
  10. ));   
  11.    
  12. //如果是activeForm,就得这么用
  13. echo $form->radioButton($model'name'array(  
  14.     'value'=>1,  
  15.     'uncheckValue'=>null  
  16. ));  
  17. echo $form->radioButton($model'name'array(  
  18.     'value'=>2,  
  19.     'uncheckValue'=>null  
  20. ));  

果然纠结。。。NND




本站采用创作共享版权协议, 要求署名、非商业和保持一致. 本站欢迎任何非商业应用的转载, 但须注明出自"易栈网-膘叔", 保留原始链接, 此外还必须标注原文标题和链接.

Tags: yii

« 上一篇 | 下一篇 »

只显示10条记录相关文章

常用网站的反向代理页[2013-09-28] (浏览: 66062, 评论: 10)
Yii CDbCriteria的常用方法 (浏览: 56342, 评论: 5)
将Yiiframework与JQuery easyUI整合使用 (浏览: 38198, 评论: 2)
Yii:relations update(self::STAT) (浏览: 33673, 评论: 0)
值得收藏的yii2的doc中关于db Query的说明 (浏览: 29239, 评论: 0)
Yii Demos 随想 (浏览: 28546, 评论: 3)
在Yii框架中使用Hprose或PHPRPC (浏览: 27555, 评论: 0)
Yii ClinkPager 郁闷 (浏览: 27277, 评论: 2)
Yiiframework(Yii框架)开发笔记:续四 (浏览: 26747, 评论: 3)
Yii 一行代码,为模块绑定子域名 (浏览: 26086, 评论: 0)

发表评论

评论内容 (必填):