【问题标题】:jQuery UI Autocomplete not working with WordpressjQuery UI 自动完成不适用于 Wordpress
【发布时间】:2015-08-05 03:45:30
【问题描述】:

我想在 Wordpress 上使用 jQuery UI 自动完成功能,但由于某种原因它不起作用。

无论如何,我会告诉你我已经拥有的:

HTML

<input type="text" name="db-search" id="db-search" autocomplete="off" />

JavaScript

$('#db-search').autocomplete({ 
    source: function (request, response) {
        $.ajax({
      type: "POST",
      url:"/wp-content/themes/your-click/autocomplete.php",
      data: { autocomplete: 'true' },
    });
      }
    }, { minLength: 1 });

PHP (autocomplete.php)

<?php
    global $wpdb;
    $string = wpdb::_real_escape( $_GET['term'] );
    $get_results = $wpdb->get_results("SELECT * FROM yc_customers WHERE website LIKE $string ORDER BY website ASC");

    $json[] = '';
    foreach ($get_results as $get_result) {
        array_push($json, $get_result->website);
    }

    echo json_encode($json);
    flush;
?>

在使用 chrome 进行测试时,我没有收到任何错误。所以我不知道我的代码有什么问题,但我猜 PHP 肯定有问题。

【问题讨论】:

    标签: php jquery wordpress jquery-ui autocomplete


    【解决方案1】:

    更新

    php 在问题似乎期待“GET”请求$_GET['term'] ?尝试用$string = wpdb::_real_escape( $_POST['autocomplete'] ); 替换$string = wpdb::_real_escape( $_GET['term'] );

    Autocomplete - Remote JSONP datsource - viewsource


    .autocomplete() 需要 response(Array) ,在 data: { autocomplete: 'true' }, 处也似乎是尾随逗号 ,

    试试

    $('#db-search').autocomplete({ 
        source: function (request, response) {
                  // input query 
                  var term = request.term;
                  $.ajax({
                    type: "POST",
                    url:"/wp-content/themes/your-click/autocomplete.php",
                    // "POST" `term` to server
                    data: { autocomplete: term }
                  }).then(function(data) {
                    response(data)
                  }, function error(jqxhr, textStatus, errorThrown) {
                    console.log(textStatus, errorThrown)
                  });
          }
        },
        minLength: 1 
    });
    

    【讨论】:

    • 谢谢,现在我收到一个错误:未捕获的类型错误:无法使用 'in' 运算符在 [""] 中搜索 '3'。它告诉我这个错误来自 jquery.min.js。它还说 response(data) (在您的代码中)是一个匿名函数。
    • 尝试打开console点击“网络”->“响应”。是否从服务器发送响应?从$.post()返回的data是一个包含空字符串[""]的数组吗?
    • 当我点击网络时,没有所谓的“响应”。我正在用 Chrome 和 Firefox 测试它。我唯一看到的是帖子已成功发送到 autocomplete.php 但它没有返回任何内容。
    • @RezaSaadati 查看更新后的帖子。在问题上注意到php 似乎期待“GET”请求$_GET['term']?尝试用$string = wpdb::_real_escape( $_POST['autocomplete'] ); 替换$string = wpdb::_real_escape( $_GET['term'] );
    • 对不起,我现在看到“响应”。它正在返回 [""]。我尝试替换您的代码,但没有帮助。我也试过这个:$string = $_GET['term'];$string = $_POST['autocomplete']; 但它仍然说response(data) 是一个匿名函数。
    猜你喜欢
    • 2020-05-05
    • 2015-10-16
    • 1970-01-01
    • 2011-10-16
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多