我必须在表单中使用文本编辑器,即tinymce。我必须在选择组合框时在文本编辑器中显示数据。只需使用textarea,就可以在选择组合时在文本区域中获取值。现在我必须使用文本编辑器而不是简单的文本区域。 我的 View 代码是:
<div class="row col2">
<?php echo $form->labelEx($model,'template'); ?>
<?php
$records = CHtml::listData(EmailTemplate::model()->findAll(array('order' => 'email_template_id','condition'=>"status= '1'")), 'email_template_id', 'subject');
// echo $form->dropDownList($model,'template',$records,array('id'=>'myDropDown','empty' => 'Select'));
echo $form->dropDownList($model,'template',$records,array(
'id' => 'myDropDown',
'empty' => 'Select Template',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('reply/description'),//your controller and action name
'update'=>'#myTextArea',
'success'=> 'function(data) {
$("#myTextArea").empty();
$("#myTextArea").val(data);
} '
)));
?>
<?php echo $form->error($model,'template'); ?>
</div>
<div class="row col2">
<?php echo $form->labelEx($model,'message'); ?>
<?php echo $form->textArea($model,'message',array('id'=>'myTextArea','style'=>'width: 680px; height: 300px;')); ?>
<?php echo $form->error($model,'message'); ?>
</div>
我的js代码如下:
<script type="text/javascript">
tinymce.init({
selector: "textarea#myTextArea",
theme: "modern",
preformatted:true,
width: 700,
height: 300,
plugins: [
"advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
"searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
"save table contextmenu directionality emoticons template paste textcolor"
],
content_css: "css/content.css",
menubar:false,
toolbar: "bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent| forecolor backcolor",
});
</script>
<script>
$('#myDropDown').on('change', function() {
$.ajax({
url: "<?php echo $this->createUrl('reply/description'); ?>",
dataType: 'html',
type: 'POST',
data: {dropDownValue: $(this).val()},
success: function(template, textStatus, jqXHR) {
alert(data);
$('#myTextArea').val(data);
}
});
});
</script>
我的 Controller 是:
public function actionDescription()
{
$cvId=0;
$cvId= $_POST['Reply']['template'];
if (Yii::app()->request->isAjaxRequest) {
if($cvId>0){
$templateModel=EmailTemplate::model()->findByPk($cvId);
echo $templateModel->description;
}
}
}
最佳答案
一旦 TinyMCE 出现在页面上,只需更改底层文本区域的内容(您在 AJAX 调用中执行的操作)不会导致 TinyMCE 重新加载内容。
您需要使用 TinyMCE 的 API 在 AJAX 调用的成功方法中设置编辑器的内容。
例如:
tinyMCE.activeEditor.setContent(data);
...其中 data 是包含 AJAX 调用返回的内容的字符串。
关于javascript - 单击组合框时在文本编辑器tinymce中显示值:Yii 1. 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35252654/