【问题标题】:select2 to redirect on clickselect2 在点击时重定向
【发布时间】:2013-08-15 19:17:01
【问题描述】:

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

我正在使用以下代码来构建选择 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;
    }
}

【问题讨论】:

    标签: php jquery json jquery-select2


    【解决方案1】:

    在发布此内容一年后遇到同样的问题并在 Google 上搜索了一些内容,结果在这里忘记了我一开始就发布过它。

    很好地找到了解决方案:

    <script>
    $(document).ready(function () {
        $("#search").select2({
            ajax: {
                url: "users.php",
                dataType: 'json',
                data: function (term) {
                    return {
                        term: term,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            }
        });
        $("#search").on("select2-selecting", function(e) {
            window.location.href = 'user_edit.php?id=' +  e.val;
        });
    });
    </script>
    

    【讨论】:

    • 这对我在 Slim2 和 Twig 中应用它有所帮助。所以它对我有用的方式是:var value = $("#projects").val(); var url = "{{ urlFor('projects.edit',{id: value }) }}"; window.location.href = url+value;。谢谢。
    • 这在将 e.val 更改为 e.params.data.id 后有效
    【解决方案2】:

    版本 4.0 +

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

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

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多