2

我正在尝试在下拉列表的值更改时验证下拉列表。我想检查表中是否已经存在所选作业状态。下面是我的代码:

<script>
     function validate_dropdown(id)
    {
        alert("Selected id = "+id);
        //var msg = <?php echo NotificationRules::model()->validate_job($_POST['id']);?>
        //alert("Message from model func = "+msg);
    }
</script>

<?php
  echo $form->dropDownList($model, 'job_status_id', $jobstatuslist ,
            array('empty'=>'Please Select job status (required)', 'onchange'=>'js:validate_dropdown(this.value)')
        );


?>

我正在尝试将 js 变量 id 传递给 php 函数并在已存在所选作业状态的条目时发回一条消息。我能够在 js 函数 validate_dropdown() 中获得选定的值,但无法继续进行。任何人请帮助......

4

2 回答 2

1

检查以下示例。在此,我在下拉列表中显示所有用户。我将用户id作为选项值,将用户名作为选项标签

用户表:

    id  username    
    ------------------
    1   Heraman
    2   Dileep  
    3   Rakesh
    4   Kumar   


    <?php
    $list=CHtml::listData(User::model()->findAll(), 'id', 'username');       
    echo CHtml::dropDownList('username', $models->username, $list, array('empty' => '---Select User---','onchange'=>'alert(this.value)'));
    ?>

在你的情况下,你可以使用

 'onchange'=>'validate_dropdown(this.value)

  //Your script
  <script>
        function validate_dropdown(id)
        {
            alert("Selected id = "+id);        
        }
    </script>
于 2013-06-04T10:53:04.410 回答
0

我通过进行 AJAX 调用解决了这个问题......

最终工作代码:

视图中的下拉代码

<?php
   echo $form->dropDownList($model, 'job_status_id', $jobstatuslist ,
             array(//AJAX CALL.
                    'prompt' => 'Please Select job status (required)',
                    'value' => '0',
                    'ajax'  => array(
                            'type'  => 'POST',
                            'url' => CController::createUrl('NotificationRules/notificationPresent/'),
                            'data' => array("job_id" => "js:this.value"),
                            'success'=> 'function(data) {
                                    if(data == 1)
                                    {
                                        alert("Rule is already present for this status, Please update existing rule.");
                                    }
                                }',
                            'error'=> 'function(){alert("AJAX call error..!!!!!!!!!!");}',
                    )//end of ajax array().
        ));

?>

控制器中的代码(动作)

<?php
   public function actionNotificationPresent()
   {
    if (Yii::app()->request->isAjaxRequest)
    {
        //pick off the parameter value
        $job_id = Yii::app()->request->getParam( 'job_id' );
        if($job_id != '')
        {
            //echo "Id is received is ".$job_id;

            $rulesModel = NotificationRules::model()->findAllByAttributes(array('job_status_id'=>$job_id));

            if(count($rulesModel))
                echo 1;
            else 
                echo 0;

        }//end of if(job_id)
        else
        {
            //echo "Id id not received";
            echo 0;

        }

    }//end of if(AjaxRequest).

}//end of NotificationPresent().

?>

现在,如果已存在任何具有选定作业状态的规则,我会收到警报。

于 2013-06-05T12:36:05.637 回答