dongmu3457 2017-01-20 20:53 采纳率: 0%
浏览 34

jQuery - 自动完成文本框,在选择1项时修改其他文本框

I'm trying to implement an autocomplete feature that gets a dataset from php and then uses that to populate the autocomplete dropdown. Right now I've got it so that when the user types in enough characters to narrow down the list to 1 item, another 2 textboxes are filled with related information.

So basically, the system works if I type in enough chars to narrow down the result to 1 row. But when I try to select an item that is listed in the drop down on the text box, the other two fields do not update.

The API documentation suggests that I need to use the jquery change event but I am not sure how to implement it. I would appreciate some help.

Here's my javascript code.

$( function()   {
    $( "#competency" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "<?php echo base_url()."index.php/Test/search"; ?>",
                dataType: "json",
                data: request,
                success: function(data) {
                    if (data.response == 'true') {
                        response(data.message);
                        if (data.found == 'true') {
                            $( '#competencyDescription' ).val(data.definition);
                            $( '#competencySource' ).val(data.src);
                        }   else    {
                            $( '#competencyDescription' ).val("");
                            $( '#competencySource' ).val("");
                        }
                    }
                }
             });
        }
    });
});

Here's my php

    public function search()    {
    // Trim data typed into text box
    $compName = trim($this->input->get('term', TRUE));
    $this->load->model('Competencies');
    $compController = new Competencies();
    $query = $compController->getCompetenicesByName($compName);
    if ($query->num_rows() == 1)    {
        $data['found'] = 'true';
    }
    if ($query->num_rows() > 0)    {
        $data['response'] = 'true';
        $data['message'] = array();
        foreach ($query->result() as $row)    {
            $data['message'][] = array(
                    'label'=>$row->CompetencyName,
                    'value'=>$row->CompetencyName
            );
            $data['definition'] = $row->CompetencyDefinition;
            $data['src'] = $row->CompetencyDefinitionSource;
        }
    }    else {
        $data['response'] = 'false';
    }
    echo json_encode($data);
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 shape_predictor_68_face_landmarks.dat
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制