1

问题: 我有一个jQuery在控制器的 add 方法中工作的脚本,但是当我将搜索功能添加到 edit 方法时,下拉菜单不起作用;但是我知道jQuery脚本正在运行,因为我收到了结果的回复。我可能忽略了一些简单的事情,但我似乎无法发现它。

控制器编辑方法:

public function edit($id = null) {
            $this->loadModel('PsychproScreening');
            $this->set('pidList', $this->PsychproScreening->find('list', array('fields' => array('pid','pid'), 
                                                        'order' => 'pid ASC')));

    if (!$this->PsychproConcomitantMed->exists($id)) {
        throw new NotFoundException(__('Invalid psychpro concomitant med'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
        if ($this->PsychproConcomitantMed->save($this->request->data)) {
            $this->Session->setFlash(__('The psychpro concomitant med has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The psychpro concomitant med could not be saved. Please, try again.'));
        }
    } else if(!$this->RequestHandler->isAjax()) {
        $options = array('conditions' => array('PsychproConcomitantMed.' . $this->PsychproConcomitantMed->primaryKey => $id));
        $this->request->data = $this->PsychproConcomitantMed->find('first', $options);
    }

            if ( $this->RequestHandler->isAjax() ) {
                debug('here');
                $this->search();
    }
}

控制器搜索方法:

private function search(){
    $this->loadModel('FdaMed');
    $this->autoRender=false;
    $meds=$this->FdaMed->find('all',array('fields' => array('med_id','non_proprietary_name', 'route_name', 'dosage_for_mname'), 
                                'conditions'=>array('FdaMed.non_proprietary_name LIKE'=>'%'.$_GET['term'].'%')));
            $i=0;
            foreach($meds as $meds){
                    $response[$i]['value']=$meds['FdaMed']['med_id'];
                    $response[$i]['label']=$meds['FdaMed']['non_proprietary_name'].' ('.
                            $meds['FdaMed']['route_name'].'/ '.$meds['FdaMed']['dosage_for_mname'].')';
            $i++;
            }
    echo json_encode($response);
}

编辑视图

<?php include_once 'concomitant_meds_inc.php';
$this->Html->addCrumb('PsychPro', '/Psychpros');
$this->Html->addCrumb('PsychPro Concomitant Med', '/PsychproConcomitantMeds');
$this->Html->addCrumb('Edit Concomitant Med', '/PsychproConcomitantMeds/edit/'.$this->request->data['PsychproConcomitantMed']['id']);
?>
<div class="psychproConcomitantMeds form">
<?php echo $this->Form->create('PsychproConcomitantMed',array('action' => 'edit')); ?>
    <fieldset>
        <legend><?php echo __('Edit Psychpro Concomitant Med'); ?></legend>
    <?php
        echo $this->Form->input('pid', array('options' => $pidList, 'empty' => 'Choose a value'));
                echo $this->Form->input('timept', array('options' => $tpt, 'empty' => 'Choose a value'));
                echo '<h3>Current Medication: '.$this->request->data['PidConcomitantMed']['non_proprietary_name'].' Route: '.
                        $this->request->data['PidConcomitantMed']['route_name']." Dosage Type: ".
                        $this->request->data['PidConcomitantMed']['dosage_for_mname'].'</h3>';
                echo $this->Form->input('med_name',array('type'=>'text','id'=>'med_name','label'=>'Medication Name (Route / Dosage Type)'));
                echo $this->Form->hidden('med_id', array('id' => 'med_id'));
        echo $this->Form->input('meds_taken');
                echo $this->Form->hidden('id', array('id' => 'p_form_id'));
                echo $this->Form->hidden('update_initials',array('value' => $this->Session->read('CoreUser.user_initials')));
                echo $this->Form->hidden('update_time_stamp', array('value' => date("Y-m-d h:i:s")));
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
    <h3><?php echo __('Actions'); ?></h3>
    <ul>

        <li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('PsychproConcomitantMed.id')), null, __('Are you sure you want to delete # %s?', $this->Form->value('PsychproConcomitantMed.id'))); ?></li>
        <li><?php echo $this->Html->link(__('List Psychpro Concomitant Meds'), array('action' => 'index')); ?></li>
    </ul>
</div>
<?php echo $this->Html->script('autocomplete_edit'); ?>

jQuery 自动完成

$(document).ready(function(){

    // Defining a placeholder text:
    $('#med_name').defaultText('Search for medications');
    console.log($('#p_form_id').val());
    var form_id = $('#p_form_id').val();

    $('#med_name').autocomplete({
    minLength       : 3,
    source          : 'http://127.0.0.1/my_cakephp/PsychproConcomitantMeds/edit/'+form_id,
    autoFocus       : true,
    select          : function(event,ui){
        //console.log(ui.item['value']);
        $('#med_id').val(ui.item['value']);
        //console.log($('#med_id'));
    }
    });
});

    // A custom jQuery method for placeholder text:

$.fn.defaultText = function(value){

        var element = this.eq(0);
        element.data('defaultText',value);

        element.focus(function(){
        if(element.val() == value){
        element.val('').removeClass('defaultText');
        }
        }).blur(function(){
        if(element.val() == '' || element.val() == value){
        element.addClass('defaultText').val(value);
        }
        });

        return element.blur();
}

调试

我知道以下工作:

  1. 使用 chrome 中的内置开发人员工具,我从jQuery带有jQuery格式的搜索结果的数组中得到响应{Value: , Label: }
  2. 我知道这段jQuery代码有效,因为我有用于 add 方法的类似代码。如果您想查看代码,请告诉我,我会更新。唯一真正的区别是我需要获取记录的 id 并将其附加到jQuery脚本中 URL 的末尾。
  3. 我在响应中得到了正确的 url。看起来像../view/21?term=insulin

更新0:

经过更多调试后,我找到了问题所在。在该方法的jQuery脚本中,add这些日志将被打印,但该edit方法没有。

$('#med_name').autocomplete({
minLength       : 3,
source          : 'http://127.0.0.1/my_cakephp/PsychproConcomitantMeds/edit/'+form_id,
autoFocus       : true,
select          : function(event,ui){
    console.log(ui.item['value']);
    $('#med_id').val(ui.item['value']);
    console.log($('#med_id'));
}
});

所以我知道搜索功能正在工作,但选择事件没有被触发。下拉菜单也没有出现,我觉得这很奇怪,因为jQuery它显然在工作,因为它得到了结果,它们只是无法显示。

4

1 回答 1

1

不完全确定我做了什么,但它现在可以工作了。以下是所做更改的文档:

autocomplete_edit.js

$(document).ready(function(){
    //console.log('AutocompleteEdit:Function=ready():Beginning');
    // Defining a placeholder text:
    $('#search_key').defaultText('Search here');
    //console.log($('#p_form_id').val());
    var form_id = $('#p_form_id').val();
    var pathname = window.location.pathname;

    $('#search_key').autocomplete({
    minLength       : 3,
    source          : 'http://127.0.0.1'+pathname+'/'+form_id,
    autoFocus       : true,
    select          : function(event,ui){
        //console.log(ui.item['value']);
        $('#search_id').val(ui.item['value']);
        $('#search_key').val(ui.item['label']);
        //console.log($('#med_id'));
    }
    });
});

我将选择器的 id 更改为更抽象,以便其他组件可以使用此自动完成功能。我添加了获取 webroot 并调用控制器/方法的路径名。我还获得了记录的 ID,因此我可以将 med_id 存储在隐藏字段中,并将药物名称保留在搜索字段中。

更新了编辑视图

echo '<h3>Current Medication: '.$this->request->data['PidConcomitantMed']['non_proprietary_name'].' Route: '.
 $this->request->data['PidConcomitantMed']['dosage_for_mname'].'</h3>';
echo $this->Form->input('med_name',array('type'=>'text','id'=>'search_key','label'=>'Medication Name (Route / Dosage Type)'));
echo $this->Form->hidden('med_id', array('id' => 'search_id'));
echo $this->Form->input('meds_taken');
echo $this->Form->hidden('id', array('id' => 'p_form_id'));
于 2013-07-26T12:47:57.600 回答