php - select2 单击时重定向

标签 php jquery json jquery-select2

我正在使用 select 2 插件来搜索用户。一切都在运行,我的 json 最终得到: [{"id":"1","text":"Alex Fagard (afagard) ID: 1"}] .etc.

我使用以下代码来构建 select 2 界面:

$(document).ready(function(){
   $('#username-search').select2({
    minimumInputLength: 2,
    select: function(event, ui) {
        AutoCompleteSelectHandler(event, ui)
    },
    ajax: {
      url: "classes/search.class.php?sType=users",
      dataType: 'json',
      data: function (term, page) {
        return {
          term: term
        };
      },
      results: function (data, page) {
        return { results: data };
      }
    }
  });
});

但是,我不知道如何做到这一点,以便当管理员选择用户(也称为单击其下拉区域)时,页面重定向到 userview.php?id=1 其中 1 是 JSON 数组中的 id。

如果有人感兴趣的话搜索功能:

public function searchUsers($term = '') {
    if (isset($term)) {
        $term = parent::secure($term);
        $params = array( ':searchQ' => $term . '%' );
        $sql = "SELECT distinct username as suggest, user_id, name
                FROM login_users
                WHERE username LIKE :searchQ
                OR name LIKE :searchQ
                OR user_id LIKE :searchQ
                ORDER BY username
                LIMIT 0, 5";

        $stmt = parent::query($sql, $params);

        if ( $stmt->rowCount() > 0 ) {
            while($suggest = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $data[] = array(
                    'id' => $suggest['user_id'],
                    'text' => $suggest['name'] . ' (' . $suggest['suggest'] . ')' . ' ID: ' . $suggest['user_id'],
                );
            }
        } else {
            $data[] = array('id'=>'0', 'text'=>'No results found!');
        }
        echo json_encode($data);
        flush();
    }
}
$searchParam = new Search();

if (isset($_GET['term'])) {
    // we secure the term before running the search and querying the database
    $term = $_GET['term'];
    switch (isset($_GET['sType']) ? $_GET['sType'] : NULL) {
        case 'users':
            $searchParam->searchUsers($term);
        break;
        case 'levels':
            $searchParam->searchLevels($term);
        break;
    }
}

最佳答案

版本 4.0 +

事件现在的格式为:select2:selecting(而不是 select2-selecting)

$("#search").on("select2:selecting", function(e) {
 window.location.href = 'user_edit.php?id=' +  e.val;
});

关于php - select2 单击时重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18221532/

相关文章:

json - 在 OData 客户端中展开 DataServiceQuery 时出错

php - 我错过了面向对象编程的重点吗?

php - 获取分组字段的计数,但也需要计数为零的字段名称

javascript - jQuery - 我的代码似乎不适用于所有浏览器

jquery - 使用 jquery 将表单值传递到 iframe 的 src url

c++ - Curl 命令行有效,C++ curl 库无效

php - vuejs 预渲染 php 动态内容

php - 适用于 PHP 的 Microsoft Azure 和 SAS

php - 使用 AJAX 动态地同时动态地创建多个动态表

json - Jackson 在将对象序列化为 json 时包含 'class' 字段