Py学习  »  Jquery

多个值不起作用的Jquery ui自动完成

Anonymous • 5 年前 • 1382 次点击  

我正在研究jquery ui的自动完成功能,但是我的要求是多个逗号分隔的值,我编写的代码只对一个值起作用,我试过这个方法,但这对我不起作用,我几乎没有这样的要求

1) when search/autocomplate  in text box it should get username and id of that user but id not show to front end just binded with user names
2)selected usernames show in text box with multiple values using comma separated (currently it is working for single user only comma is appending but multiple values not)
3) whenever selected any username  id associated with that user append into hidden field  " name='hidden_id[]'  "

<input type="text" name="search_val" class="form-control search_val"/>

<input type="hidden" name="hidden_id[]" class="hidden_id" value=""/>

<script>
$(function () {
    $(document).ready(function () {
        function split(val) {
            return val.split(/,\s*/);
        }
        function extractLast(term) {
            return split(term).pop();
        }
        $(".search_asset").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "<?php echo SITE_URL . 'ajax/get_users'; ?>",
                    dataType: "json",
                    data: {
                        term: request.term, request: 1
                    },
                    success: function (data) {
                        response(data);
                    }
                });
            },

            select: function (event, ui) {
                var terms = split(this.value);
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push(ui.item.value);
                // add placeholder to get the comma-and-space at the end
                terms.push("");
                this.value = terms.join(",");
                return false;
            }
        });

    });


    public function get_users() {
        $term = $_GET['term'];
        $result = $this -> db -> query("select userid,username from users WHERE username LIKE '%".$term."%' ") -> result();
        if (count($result) > 0) {
            foreach($result as $row)
            $arr_result[] = $row -> username;
            $arr_result[] = $row -> userid;
            echo json_encode($arr_result);
        }
    }
});

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54249
 
1382 次点击  
文章 [ 1 ]  |  最新文章 5 年前