无法验证我的静态选项按钮列表

时间:2012-06-07 12:34:55

标签: php yii

我在页面中创建了一个静态选项列表,我想使用JavaScript而不是AJAX进行验证:

<?php echo $form->labelEx($model,'emlevel'); ?>
<?php 
   echo $form->radioButtonList($model,'emlevel',
                  array('L'=>'Low','M'=>'Moderate','X'=>'Low Moderate', 'H'=>'High'),
                  array('separator' => "  " ));
?>
<?php echo $form->error($model,'emlevel'); ?> 





function chk()
{ 
    // first I checked the value for emlevel to verify it get the value or not 
    //but it shows undefined
    alert (document.forms["ConsultationNew"] ["ConsultationNew[enc_type]"].value);
    if (document.forms["ConsultationNew"]["ConsultationNew[emlevel]"].value == null) { 
        alert ('choose one EMlevel'); return false; 
    } 
}   

我无法通过document.getelement ....价值获得价值。它显示undefined

1 个答案:

答案 0 :(得分:1)

您可能想要使用CActiveForm。客户端验证配置如下:

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableAjaxValidation'=>false,
'enableClientValidation'=>true,
    'clientOptions' => array (
        'validateOnSubmit' => true,
        'validateOnChange' => true,
        'validateOnType' => true,
    ),
)); ?>

当你在Yii工作时,我肯定会使用CActiveForm进行客户端验证(以及Ajax和服务器端验证)。它就像一个魅力。你必须有更好的事情要做,而不是重新编写Javascript中的编程验证。