我正在尝试将下拉列表验证为下拉列表更改的值。我想检查表中是否已存在所选作业状态。 以下是我的代码:
<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()中获取所选值,但无法继续进行。任何人请帮助.......
答案 0 :(得分: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>
答案 1 :(得分: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().
?>
现在,如果已经有选定作业状态的规则,我将收到提醒。