搜索期间AJAX数据不会加载到多重选择框中

时间:2019-01-02 19:01:23

标签: php jquery ajax jquery-select2

我已经创建了一个关键字搜索框,我想尝试使用JSON / AJAX搜索要输入的关键字。

在过去的2个小时里,我尝试了所有尝试,但都徒劳地尝试了,但我知道我遗漏了一些东西,但不能指责。我只收到一条消息,“无法加载结果”(此消息来自select2的cdnjs)。当我加载keyword_search.php页面时,我得到了预期格式的结果(请参见下面的代码):

我创建了一个多选框

    WebDriver driver = new InternetExplorerDriver();


    driver.get("https://ekrs.ms.gov.pl/rdf/pd/");

    WebElement krsinput = driver.findElement(By.name("unloggedForm:krs2"));
    krsinput.sendKeys(String.valueOf(myNumber);

    WebElement szukajbutton = driver.findElement(By.name("unloggedForm:submit"));
    szukajbutton.click();

我的JavaScript代码是:

<select class="js-example-basic-multiple" multiple="multiple" name="img_keywords[]" style="width:100%">
                <?php
                    $path = $_SERVER['DOCUMENT_ROOT'];
                    $path .= "/includes/connections/mysqli.php";
                    require($path);

                    $sql = "SELECT * 
                            FROM tbl_photos_keywords_all
                            GROUP BY pa_keyword
                            ";

                    $data = $link->query($sql);

                    $keywords = $row['img_keywords'];
                    $selected = explode(',', $keywords);

                    while($row2 = $data->fetch_assoc()) {

                            $isSelected = in_array($row2['pa_keywordID'], $selected) ? "selected='selected'" : "";

                            echo "<option {$isSelected} value='{$row2['pa_keywordID']}'>{$row2['pa_keyword']}</option>";

                    }
                        $link->close();
                ?>
    </select>

我的keyword_search.php文件:

<script>
       $(document).ready(function() {
        $('.js-example-basic-multiple').select2({
            tags: true,
            tokenSeparators: [","],
            createSearchChoice: function (term, data) {

                if (term.trim().length > 0) {
                    if ($(data).filter(function () {
                      return this.text.toLowerCase().localeCompare(term.toLowerCase()) === 0;
                    }).length === 0) {
                        return {
                            id: term,
                            text: term,
                            isNew: true // this is necessary to check if the item is newly added or not
                        };
                    }
                }
            },
            multiple: true,
            minimumInputLength: 1,
            allowClear: true,    

            ajax: {
                url: "keyword_search.php",
                dataType: 'json',
                type: "GET",
                quietMillis: 50,
                data: function (term) {
                    return {
                        term: name
                    };
                },
                results: function (data) {
                    return {
                        results: $.map(data, function (item) {
                            return {
                                id: item.id,
                                text: item.name
                            }
                        })
                    };
                }
            }
    }
            );
            });

    </script>

0 个答案:

没有答案