手机浏览 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条记录相关文章

php pack()函数详解与示例 (浏览: 63239, 评论: 6)
mongodb删除索引 (浏览: 39964, 评论: 0)
linux 查找目录或文件 (浏览: 36721, 评论: 1)
RabbitVCS 用后感 (浏览: 31644, 评论: 0)
Yiiframework(Yii框架)开发笔记:续四 (浏览: 28196, 评论: 3)
看上去不错:Chrome 扩展 Monster (浏览: 25377, 评论: 2)
redis:Error: read error on connection (浏览: 24005, 评论: 0)
yhustc:Twisted+AC自动机构建高效的过滤服务器 (浏览: 23981, 评论: 2)
项目管理流程图 (浏览: 23221, 评论: 0)
开发笔记记录 (浏览: 21613, 评论: 0)

发表评论

评论内容 (必填):