我有一个Yii中的DropDownList和文本字段,当我选择下拉列表中的项目,这个名字应该显示在文本框。我想这个概念使用Ajax和但only.I在这里贴我的code页面刷新后更新,敬请期待过和建议我如何设置 每一个直系选择下拉列项后,文本框。
以下code所在的保护/视图/表
< TD>
<?PHP的echo $形式 - > labelEx(ScriptQuestion ::模型(),田); ?>< / TD>< TD>
<?PHP的回声cHTML等:: activedropDownList(ScriptQuestion ::模型(),crm_base_contact_form_field_id',$ select_field,
阵列(
'ID'=> send_bcfield,
类=> col_165,
AJAX=>阵列(
'类型'=> POST,
'URL'=> CController :: createUrl('DisplayMessage'),
更新=> #question_editor,
'数据'=>阵列('bcfield'=>JS:THIS.VALUE),
'成功'=> 功能(数据){$(#question_editor)空()。
$(#question_editor)VAL(数据)。
}',
))
); ?>
< / TD>
< TD>
<?PHP的echo $形式 - >的textarea($模式,信息,阵列('身份证'=>question_editor,最大长度=> 508)); ?>
< / TD>
这是控制器操作:
公共职能actionDisplayMessage(){
$ Q = $ _ POST ['bcfield'];
$模式= ScriptQuestion ::模型() - >的findAll();
$ SQL =选择从crm_field名在crm_field_id =。 $ Q;
$命令=的Yii ::应用程序() - > DB-> createCommand($ SQL);
$结果= $命令 - > queryScalar();
回声%$结果%。;
$这个 - > performAjaxValidation($模式);
}
解决方案
没有必要阿贾克斯,它只是一个简单的使用Javascript / jQuery的
只是这样做(与你的ckeditor的实例名称替换editor1):
<脚本>
$(#send_bcfield)。改变(函数(){
VAR选择= $(#send_bcfield选项:选择)VAL()。
CKEDITOR.instances.editor1.setData(选择);
});
< / SCRIPT>
或者改变你的code到这一点:
< PHP
回声CHTML :: activedropDownList(ScriptQuestion ::模型(),crm_base_contact_form_field_id',$ select_field,阵列(
'ID'=> send_bcfield,
类=> col_165,
的onchange'=> '$(#send_bcfield)。改变(函数(){
VAR选择= $(#send_bcfield选项:选择)VAL()。
CKEDITOR.instances.editor1.setData(选择);
});',
)
);
?>
更新:我改变了我的code为更改ckeditor的值根据我的回答如下您的评论
I have a Yii dropdownlist and text field, when i select the dropdown list item, this name should be displayed in textfield. I tried this concept using ajax with But it updates after page refresh only.I have pasted my code here, please look-through and suggest me how to set textfield after every immediate selection of drop down listed item.
The following code resides protected/views/form
<td>
<?php echo $form->labelEx( ScriptQuestion::model(),'Field'); ?></td><td>
<?php echo CHtml::activedropDownList( ScriptQuestion::model(),'crm_base_contact_form_field_id',$select_field,
array(
'id' => 'send_bcfield',
'class' => 'col_165',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('DisplayMessage'),
'update' => '#question_editor',
'data' => array('bcfield' => 'js:this.value'),
'success'=> 'function(data) {$("#question_editor").empty();
$("#question_editor").val(data);
} ',
))
); ?>
</td>
<td>
<?php echo $form->textArea($model, 'message', array('id'=>'question_editor','maxlength'=>508, )); ?>
</td>
This is controller action:
public function actionDisplayMessage(){
$q = $_POST['bcfield'];
$model=ScriptQuestion::model()->findAll();
$sql = "SELECT name FROM crm_field WHERE crm_field_id=". $q ;
$command = Yii::app()->db->createCommand($sql);
$result= $command->queryScalar();
echo "%".$result."%";
$this->performAjaxValidation($model);
}
解决方案
No need to Ajax, it's just a simple javascript/jQuery .
Just do this ( replace editor1 with your ckeditor instance name) :
<script>
$("#send_bcfield").change(function(){
var selected = $("#send_bcfield option:selected").val();
CKEDITOR.instances.editor1.setData(selected);
});
</script>
Or change your code to this :
<?php
echo CHtml::activedropDownList(ScriptQuestion::model(), 'crm_base_contact_form_field_id', $select_field, array(
'id' => 'send_bcfield',
'class' => 'col_165',
'onchange' => '$("#send_bcfield").change(function(){
var selected = $("#send_bcfield option:selected").val();
CKEDITOR.instances.editor1.setData(selected);
});',
)
);
?>
Update : I changed my code to changing a ckeditor value according to your comment below my answer.
相关推荐
最新文章