2014-01-28 98 views
3

我有一個Yii下拉列表和文本框,當我選擇下拉列表項時,這個名字應該顯示在文本框中。我嘗試使用ajax這個概念,但它更新後頁面刷新only.I已粘貼我的代碼在這裏,請透視,並建議我如何設置 textfield後立即選擇下拉列表項目。如何在沒有刷新頁面的情況下更新dropdownlist的textfield onchange?

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> 

這是控制器動作:

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);  
    } 
+0

你必須發送Ajax請求'onchange' –

+0

你的意思是不是成功的onchange? – Bala

+0

嘗試在'成功'部分提醒'數據',並告訴我它是什麼 –

回答

2

無需阿賈克斯,它只是一個簡單的JavaScript /jQuery的

只是這樣做(與你的CKEditor的實例名稱替換editor1):

<script> 
$("#send_bcfield").change(function(){ 
    var selected = $("#send_bcfield option:selected").val(); 
    CKEDITOR.instances.editor1.setData(selected); 
}); 
</script> 

或者你的代碼改成這樣:

<?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); 
        });', 
    ) 
); 
?> 

更新:我改變了我的代碼改變CKEditor的值根據您的評論我的答案。

+0

我試過這個,但不起作用。頁面刷新後也會更改。這個文本字段ID「question_editor」的一件事與CKEDITOR集成在一起。這是否會導致問題? – Bala

+0

也許,試着把一個簡單的textarea與'id ='question_editor'' –

+0

是的,如果我試過簡單的文本區域,這將得到更新的下拉列表項目的選擇。但是我可以用ckeditor做什麼?請幫我實現這一點。我需要與ckeditor文本區域 – Bala

1

可能你只需要它添加到了CHtml :: activedropDownList設置:

'onchange' => "$('question_editor').val($('#send_bcfield option:selected').text())" 
+0

我回答的確實和你一樣,但是我讀了最後一個問題的一部分,我發現OP不希望在textarea中顯示selectbox的確切值。他想在查詢等後將其更改並刪除我的答案 –

+0

'當我選擇下拉列表項目時,此名稱應顯示在textfield.'如果不是這樣我不確定我應該說什麼 – sakhunzai

+0

I知道,他問這個問題awefull,我取消了我的答案 –

相關問題