更改字段选项时如何在 cakephp 中检索数据<select>?



我真的是个新手。我有两个名为"问题"one_answers"答案"的控制器。在答案视图中,我有一个admin_add.ctp文件,其中包括一个显示问题列表的选择框。但问题是,当有人选择问题时,它会根据所选的question_id在不同的输入字段中显示所有答案,尽管我的admin_add.ctp包含一个输入字段。因此,当有人进行更改时,必须在admin_add-ctp中动态添加字段。我的答案表包含一个字段"question_id"作为foreign_key。这是我的admin_add.ctp文件。。

<?php echo $this->
    Html->script(array('answers_add','jquery-ui-1.8.18.custom.min'), array('inline'
    => false)); ?>
    <div class="answers form">
        <?php echo $this->
            Form->create('Answer');?>
            <fieldset>
                <legend>
                    <?php echo __( 'Admin Add Answer'); ?>
                </legend>
                <?php echo $this->
                    Form->input('question_id', array('empty'=>'Please select the question'));?>
                    <span class="labelToAnswerBox">
                        Add your answers
                    </span>
                    <ul id="allAnswerHolder">
                        <li class="eachAnswer">
                            <?php echo $this->
                                Form->input('answer', array('id' => false, 'class' => 'answerInput', 'name'
                                => 'data[Answer][answer][]','div' => false, 'label' => false, 'after' =>
                                '
                                <input type="button" value="X" class="removeAnswer">
                                ', 'before' => '
                                <span class="ui-icon ui-icon-arrowthick-2-n-s">
                                </span>
                                ')); ?>
                        </li>
                    </ul>
                    <span class="addAnswer">
                        Add
                    </span>
                    <?php echo $this->
                        Form->input('Status', array('type'=>'hidden', 'id'=>'status')); echo $this->Form->input('Status1',
                        array('type'=>'hidden', 'id'=>'status1')); ?>
            </fieldset>
            <?php echo $this->
                Form->end(__('Submit'));?>
    </div>
    <?php echo $this->
        element('left');?>  

有人能帮我吗?提前谢谢。

尝试如下:

为带有AJAX请求的select框编写change()方法:

$('select#AnswerQuestionId').on('change', function() {
   var questionID = $(this).val();
   // send ajax
   $.ajax({
      url: 'admin/answers/add/' + questionID,
      method: 'get',
      dataType: 'json',
      success: function(response) {
          // for example
          // response = [{'Answer': {'id': 1, 'answer': 'ans 1'} }, {'Answer': {'id': 2, 'answer' : 2}}....];
          // now loop over the response
          var html = '';
          $.each(response, function(index, val) {
              html + = '<div id="answer_'+ val.Answer.id +'">'+ val.Answer.answer +'</div>'
          });
          // append this html to target container
          $('#target_container').append(html);
      }
   });
});

在CakePHP方面尝试如下:

public function admin_add($id = null) {
    if( !empty($id) )
    {
        $this->Answer->recursive = -1;
        $answers = $this->Answer->find('all', 
                                        array(
                                         'fields' => array('Answer.answer', 'Answer.id'), // assume the answer contains in answer field in db
                                         'conditions' => array('Answer.question_id' => $id)
                                        )
                                      );
        echo json_encode( $answers );
    }
}

最新更新