【问题标题】:jQuery Multiple Autocomplete with c#jQuery 多重自动完成与 c#
【发布时间】:2011-08-14 17:56:32
【问题描述】:

我正在使用 jQuery UI 实现多个自动完成功能。另外我正在使用网络服务来传递值。我检查了多个自动完成并使用完全相同的代码的 jQuery UI 演示,它一直工作到第一个自动完成并在最后添加一个“,”。

但我在这里卡住了,它不是在搜索下一个自动完成功能。好吧,我没有添加演示中的部分代码,我不知道它应该去哪里。如果有人可以帮助我。

听我的代码

<script type="text/javascript">
    $(function () {

        function split(val) {
            return val.split(/,\s*/);
        }
        function extractLast(term) {
            return split(term).pop();
        }

        $(".tb")

        // don't navigate away from the field on tab when selecting an item
        .bind("keydown", function (event) {
            if (event.keyCode === $.ui.keyCode.TAB &&
                    $(this).data("autocomplete").menu.active) {
                event.preventDefault();
            }
        })

        .autocomplete({

            source: function (request, response) {
                $.ajax({
                    url: "EmployeeList.asmx/FetchEmailList",
                    data: "{ 'mail': '" + request.term + "' }",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function (data) { return data; },
                    success: function (data) {
                        //term: extractLast(request.term),
                        response($.map(data.d, function (item) {
                            return {
                                value: item.Email
                            }
                        }))
                    },

                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            },

            search: function () {
                // custom minLength
                var term = extractLast(this.value);
                alert(term);
                if (term.length < 2) {
                    return false;
                }
            },
            focus: function () {
                // prevent value inserted on focus
                return false;
            },
            select: function (event, ui) {
                var terms = split(this.value);
                alert(terms);
                // 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;
            } //,

            //minLength: 2
        });
    });
</script>

这是我在 jQuery UI 演示中缺少的代码

.autocomplete({
            source: function( request, response ) {
                $.getJSON( "search.php", {
                    term: extractLast( request.term )
                }, response );
            },

我不知道在哪里添加这段代码: term: extractLast(request.term) 这是整个代码的链接Demo Code link

【问题讨论】:

    标签: c# jquery autocomplete


    【解决方案1】:

    替换

    data: "{ 'mail': '" + request.term + "' }"
    

     data: "{ 'mail': '" + extractLast(request.term) + "' }"
    

    这应该可以,但我不太喜欢你编码 JSON 的方式,可能会产生引号等问题。

    【讨论】:

    • 谢谢,成功了!顺便说一句,有什么建议您必须对 JSON 进行编码吗?
    • 嗯,类似于 JSON.stringify({mail: extractLast(request.term)}),但请确保包含来自 github.com/douglascrockford/JSON-js/blob/master/json2.js 的 JSON2.js,因为旧版浏览器可能不支持 JSON 编码。
    • 感谢您的建议。
    猜你喜欢
    • 2013-01-06
    • 2013-04-24
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 2018-06-17
    相关资源
    最近更新 更多