简体   繁体   English

jQuery自动完成:进行ajax调用以从自动完成中收集所选项目的更多详细信息

[英]JQuery Autocomplete: make ajax call to collect more details of the selected item from autocomplete

What I am trying to do is once a user selects a result from the autocomplete, an ajax call is made to a php file to collect more details about the selected item and then the details coming within response are displayed in a textarea box. 我想做的是,一旦用户从自动完成功能中选择了一个结果,就会对php文件进行ajax调用,以收集有关所选项目的更多详细信息,然后响应中的详细信息就会显示在textarea框中。

$(document).ready(function() {
    $("#bookSearch").autocomplete({
        source: "getBooks.php",
        minLength: 3,
        select: function() {
            $.ajax({
                method: "get",
                url: "getBooks2.php"
            })
            .done(function(event, ui){
                $('#bookResults').val(ui.item.value + "\n");
            })
        }
    });
});

This is my javascript at the moment. 这是我目前的javascript。 I get an error 我得到一个错误

"Cannot read property 'value' of undefined" in reference to "$('#bookResults').val(ui.item.value + "\\n");". 引用“ $('#bookResults')。val(ui.item.value +“ \\ n”);”的“无法读取未定义的属性'value'”。

How can I implement the ajax call into the autocomplete function? 如何在自动完成功能中实现Ajax调用?

<div id="autoComplete">
    <h2 id="searchTitle">Book search</h2>
    <input type="text" id="bookSearch" placeholder="Type in a book name"/>
    <br>
    <textarea rows="10" cols="40" id="bookResults" readonly></textarea>
</div>

Acccording to the documentation , callback function ( done() ) has only one parameter - data. 根据文档 ,回调函数( done() )只有一个参数-数据。 According to this, 根据这个,

.done(function(event, ui) {

should be replaced with 应该替换为

.done(function(ui) {

After some tinkering I have managed to find a solution: 经过一番修补后,我设法找到了解决方案:

$(document).ready(function() {
    $("#bookSearch").autocomplete({
        source: "getBooks.php",
        minLength: 3,
        select: function(event, ui) {
            $.ajax({
                method: "get",
                url: "getBooks2.php",
                data: {bookTitle: ui.item.value}
            })
                .done(function(data) {
                    $('#bookResults').val(data);
                })
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM