【问题标题】:datatable.js stop working if autocomplete starts如果自动完成启动,datatable.js 将停止工作
【发布时间】:2013-09-10 18:50:30
【问题描述】:

我写过 1) 使用 datatable.js 显示数据的脚本
2) 自动补全方法

  1. PortletDraggable.init();
  2. FormFileUpload.init();
  3. EducationalQualification.init();
  4. 工作经验.init();
  5. Supervisions.init();
  6. Awards.init();
  7. Publication.init();

所有这些都是正常工作的数据表方法。但是当我添加 jquery.auto-complete.js 脚本引用时,这些方法停止工作并且自动完成开始工作。

哪里有问题,请指教。

<script>

    $('.date-picker').datepicker({
        format: 'mm/dd/yyyy'
    });


    //  var oTable = null;

    jQuery(document).ready(function () {
        App.init();
        PortletDraggable.init();
        FormFileUpload.init();
        EducationalQualification.init();
        WorkExperience.init();
        Supervisions.init();
        Awards.init();
        Publication.init();
    });

        var tmpMem = 0; var tmpAward = 0; var tmpPub = 0;
        // Js For Autocomplete
        $('input[name=SearchUser]').autocomplete({

            source: function (request, response) {
                tmpMem = 0; tmpAward = 0; tmpPub = 0;
                $.ajax({
                    type: 'GET',
                    url: '@Url.Action("SearchUser", "Profile")',
                    data: { FirstName: $("#SearchUser").val() },
                    //  url: "/Profile/SearchUser?FirstName=" + $("#SearchUser").val(),
                    contentType: "application/json; charset=utf-8",
                    //       dataType: "json",
                    beforeSend: function () {
                        // code to show loading image
                        //$(".ui-content").css("opacity", "0.4");
                        //$("#imgLoader").attr("style", "display:block");
                        //var df = document.getElementById('imgLoader');
                        //df.style.position = 'absolute';
                        //var wd = ($(window).width() / 2) - 45;
                        //var hg = ($(window).height() / 2) - 60;
                        //df.style.left = wd + "px";
                        //df.style.top = hg + "px";
                    },
                    complete: function () {
                         //hide loading image
                        $(".ui-content").css("opacity", "1.0");
                        $("#imgLoader").attr("style", "display:none");
                    },
                    success: function (data) {
                           alert(data.data[0].Fname);
                        response($.map(data.data, function (item) {
                            return {
                                Sn: item.Sn, Fname: item.Fname, Mname: item.Uname, Lname: item.Lname, Title: item.Title,
                                Image: item.Image, Organization: item.Organization, JobTitle: item.JobTitle,
                                Awardname: item.Awardname, AwardYear: item.AwardYear, Pubname: item.Pubname, Pubtype: item.Pubtype, TabType: item.TabType
                            }
                        }))
                    }
                })
            },
            select: function (event, ui) {
                //var domainURL = $("#domainurl").val();
                //window.location = domainURL + "/home/NeighborProfile?id=" + ui.item.id;
                // alert(ui.item.Sn);
            }
        }).data("autocomplete")._renderItem = function (ul, item) {
            //     alert(item);="

            if (item.TabType == "member") {
                if (tmpMem == 0) {
                    tmpMem = 1;
                    return $("<li  class='SearchUserLi'></li>").data("item.autocomplete", item).append(

                   "<a href='  ' class='SearchUserLink'>" +
                       "<img  src='" + item.Image + "' alt='" + item.Fname + "' class='SearchUserImg'>" +
                       "<span class='searchUserLight'>" +
                             item.Title + ". " + item.Fname +
                             "<span style=' font-weight: bold;color:black'>" + "  " + item.Organization + "</span>" +
                       "</span>" +
                       "<span style='  font-size : 10px; color: #3B5998;'>" + item.JobTitle + "</span>" +
                   "</a>"
                   ).prepend("<span class='SearchUsercategory'> Members  </span>").appendTo(ul);
                }
                return $("<li   class='SearchUserLi'></li>").data("item.autocomplete", item).append(
                      "<hr class='SearchUserHr'>" +
                    "<a href='  ' class='SearchUserLink'>" +
                        "<img  src='" + item.Image + "' alt='" + item.Fname + "' class='SearchUserImg'>" +
                        "<span class='searchUserLight'>" +
                              item.Title + ". " + item.Fname +
                              "<span style=' font-weight: bold;color:black'>" + "  " + item.Organization + "</span>" +
                        "</span>" +
                        "<span style='  font-size : 10px; color: #3B5998;'>" + item.JobTitle + "</span>" +
                    "</a>"
                    ).appendTo(ul);
            }
            else if (item.TabType == "award") {
                if (tmpAward == 0) {
                    tmpAward = 1;
                    return $("<li style='padding:0px!important;height:80px'></li>").data("item.autocomplete", item).append(

                   "<a href='  ' class='SearchUserLink'>" +
                       "<img  src='" + item.Image + "' alt='" + item.Fname + "' class='SearchUserImg'>" +
                       "<span class='searchUserLight'>" +
                             item.Title + ". " + item.Fname +
                             "<span style=' font-weight: bold;color:black'>" + "  " + item.Organization + "</span>" +
                       "</span>" +
                       "<span style='  font-size : 10px; color: #3B5998;'>" + "  " + item.Awardname + "(" + item.AwardYear + ")" + "</span>" +
                   "</a>"
                   ).prepend("<span class='SearchUsercategory'> Awards  </span>").appendTo(ul)
                }
                return $("<li style='padding:0px!important;height:80px'></li>").data("item.autocomplete", item).append(
                     "<hr class='SearchUserHr'>" +
                    "<a href='  ' class='SearchUserLink'>" +
                        "<img  src='" + item.Image + "' alt='" + item.Fname + "' class='SearchUserImg'>" +
                        "<span class='searchUserLight'>" +
                              item.Title + ". " + item.Fname +
                              "<span style=' font-weight: bold;color:black'>" + "  " + item.Organization + "</span>" +
                        "</span>" +
                        "<span style='  font-size : 10px; color: #3B5998;'>" + "  " + item.Awardname + "(" + item.AwardYear + ")" + "</span>" +
                    "</a>"
                    ).appendTo(ul);

            }
            else if (item.TabType == "publications") {
                if (tmpPub == 0) {
                    tmpPub = 1;
                    return $("<li style='padding:0px!important;height:80px'></li>").data("item.autocomplete", item).append(

                   "<a href='  ' class='SearchUserLink'>" +
                       "<img  src='" + item.Image + "' alt='" + item.Fname + "' class='SearchUserImg'>" +
                       "<span class='searchUserLight'>" +
                             item.Title + ". " + item.Fname +
                             "<span style=' font-weight: bold;color:black'>" + "  " + item.Organization + "</span>" +
                       "</span>" +
                       "<span style='  font-size : 10px; color: #3B5998;'>" + "  " + item.Pubname + "," + item.Pubtype + "</span>" +
                   "</a>"
                   ).prepend("<span class='SearchUsercategory'> Publications  </span>").appendTo(ul)
                }
                return $("<li style='padding:0px!important;height:80px'></li>").data("item.autocomplete", item).append(
                      "<hr class='SearchUserHr'>" +
                    "<a href='  ' class='SearchUserLink'>" +
                        "<img  src='" + item.Image + "' alt='" + item.Fname + "' class='SearchUserImg'>" +
                        "<span class='searchUserLight'>" +
                              item.Title + ". " + item.Fname +
                              "<span style=' font-weight: bold;color:black'>" + "  " + item.Organization + "</span>" +
                        "</span>" +
                        "<span style='  font-size : 10px; color: #3B5998;'>" + "  " + item.Pubname + "," + item.Pubtype + "</span>" +
                    "</a>"
                    ).appendTo(ul);

            }
        };



</script>

【问题讨论】:

  • 任何错误?? console.log()??!!
  • 您能否在文档就绪功能中添加您的自动完成块。让它正确初始化。
  • @Amit - 仍然无法正常工作
  • 请检查自动完成 js link ,如果我删除 /* docs.jquery.com/UI/Widget /* 下的第二个 jquery 块,其他部分工作但自动完成停止

标签: jquery-ui jquery


【解决方案1】:

在包含 jQuery 之后,在紧跟其后的脚本标记中添加以下内容。希望这能解决您的问题。

jQuery.noConflict();

【讨论】:

  • 你应该将你的 html 和 javascript 代码发布在 fiddle 的各个块中,以使其以某种方式工作。
  • 我使用 ASP.net MVC3 框架作为后端(获取 JSON 数据),您可以在 research.athahsoftech.com/profile 处查看 html 源代码
【解决方案2】:

我不确定您的日期选择器或自动完成功能会如何工作。当 DOM 可能还没有准备好时,它们都在向 DOM 元素添加功能。试试这样的:

<script>

    var initFields = function() {

        //Date Picker
        $('.date-picker').datepicker({
            format: 'mm/dd/yyyy'
        });

        //Autocomplete
        var tmpMem = 0; var tmpAward = 0; var tmpPub = 0;
        // Js For Autocomplete
        $('input[name=SearchUser]').autocomplete({

            source: function (request, response) {
                tmpMem = 0; tmpAward = 0; tmpPub = 0;
                $.ajax({
                    type: 'GET',
                    url: '@Url.Action("SearchUser", "Profile")',
                    data: { FirstName: $("#SearchUser").val() },
                    //  url: "/Profile/SearchUser?FirstName=" + $("#SearchUser").val(),
                    contentType: "application/json; charset=utf-8",
                    //       dataType: "json",
                    beforeSend: function () {
                        // code to show loading image
                        //$(".ui-content").css("opacity", "0.4");
                        //$("#imgLoader").attr("style", "display:block");
                        //var df = document.getElementById('imgLoader');
                        //df.style.position = 'absolute';
                        //var wd = ($(window).width() / 2) - 45;
                        //var hg = ($(window).height() / 2) - 60;
                        //df.style.left = wd + "px";
                        //df.style.top = hg + "px";
                    },
                    complete: function () {
                         //hide loading image
                        $(".ui-content").css("opacity", "1.0");
                        $("#imgLoader").attr("style", "display:none");
                    },
                    success: function (data) {
                           alert(data.data[0].Fname);
                        response($.map(data.data, function (item) {
                            return {
                                Sn: item.Sn, Fname: item.Fname, Mname: item.Uname, Lname: item.Lname, Title: item.Title,
                                Image: item.Image, Organization: item.Organization, JobTitle: item.JobTitle,
                                Awardname: item.Awardname, AwardYear: item.AwardYear, Pubname: item.Pubname, Pubtype: item.Pubtype, TabType: item.TabType
                            }
                        }))
                    }
                })
            },
            select: function (event, ui) {
                //var domainURL = $("#domainurl").val();
                //window.location = domainURL + "/home/NeighborProfile?id=" + ui.item.id;
                // alert(ui.item.Sn);
            }
        }).data("autocomplete")._renderItem = function (ul, item) {
            //     alert(item);="

            if (item.TabType == "member") {
                if (tmpMem == 0) {
                    tmpMem = 1;
                    return $("<li  class='SearchUserLi'></li>").data("item.autocomplete", item).append(

                   "<a href='  ' class='SearchUserLink'>" +
                       "<img  src='" + item.Image + "' alt='" + item.Fname + "' class='SearchUserImg'>" +
                       "<span class='searchUserLight'>" +
                             item.Title + ". " + item.Fname +
                             "<span style=' font-weight: bold;color:black'>" + "  " + item.Organization + "</span>" +
                       "</span>" +
                       "<span style='  font-size : 10px; color: #3B5998;'>" + item.JobTitle + "</span>" +
                   "</a>"
                   ).prepend("<span class='SearchUsercategory'> Members  </span>").appendTo(ul);
                }
                return $("<li   class='SearchUserLi'></li>").data("item.autocomplete", item).append(
                      "<hr class='SearchUserHr'>" +
                    "<a href='  ' class='SearchUserLink'>" +
                        "<img  src='" + item.Image + "' alt='" + item.Fname + "' class='SearchUserImg'>" +
                        "<span class='searchUserLight'>" +
                              item.Title + ". " + item.Fname +
                              "<span style=' font-weight: bold;color:black'>" + "  " + item.Organization + "</span>" +
                        "</span>" +
                        "<span style='  font-size : 10px; color: #3B5998;'>" + item.JobTitle + "</span>" +
                    "</a>"
                    ).appendTo(ul);
            }
            else if (item.TabType == "award") {
                if (tmpAward == 0) {
                    tmpAward = 1;
                    return $("<li style='padding:0px!important;height:80px'></li>").data("item.autocomplete", item).append(

                   "<a href='  ' class='SearchUserLink'>" +
                       "<img  src='" + item.Image + "' alt='" + item.Fname + "' class='SearchUserImg'>" +
                       "<span class='searchUserLight'>" +
                             item.Title + ". " + item.Fname +
                             "<span style=' font-weight: bold;color:black'>" + "  " + item.Organization + "</span>" +
                       "</span>" +
                       "<span style='  font-size : 10px; color: #3B5998;'>" + "  " + item.Awardname + "(" + item.AwardYear + ")" + "</span>" +
                   "</a>"
                   ).prepend("<span class='SearchUsercategory'> Awards  </span>").appendTo(ul)
                }
                return $("<li style='padding:0px!important;height:80px'></li>").data("item.autocomplete", item).append(
                     "<hr class='SearchUserHr'>" +
                    "<a href='  ' class='SearchUserLink'>" +
                        "<img  src='" + item.Image + "' alt='" + item.Fname + "' class='SearchUserImg'>" +
                        "<span class='searchUserLight'>" +
                              item.Title + ". " + item.Fname +
                              "<span style=' font-weight: bold;color:black'>" + "  " + item.Organization + "</span>" +
                        "</span>" +
                        "<span style='  font-size : 10px; color: #3B5998;'>" + "  " + item.Awardname + "(" + item.AwardYear + ")" + "</span>" +
                    "</a>"
                    ).appendTo(ul);

            }
            else if (item.TabType == "publications") {
                if (tmpPub == 0) {
                    tmpPub = 1;
                    return $("<li style='padding:0px!important;height:80px'></li>").data("item.autocomplete", item).append(

                   "<a href='  ' class='SearchUserLink'>" +
                       "<img  src='" + item.Image + "' alt='" + item.Fname + "' class='SearchUserImg'>" +
                       "<span class='searchUserLight'>" +
                             item.Title + ". " + item.Fname +
                             "<span style=' font-weight: bold;color:black'>" + "  " + item.Organization + "</span>" +
                       "</span>" +
                       "<span style='  font-size : 10px; color: #3B5998;'>" + "  " + item.Pubname + "," + item.Pubtype + "</span>" +
                   "</a>"
                   ).prepend("<span class='SearchUsercategory'> Publications  </span>").appendTo(ul)
                }
                return $("<li style='padding:0px!important;height:80px'></li>").data("item.autocomplete", item).append(
                      "<hr class='SearchUserHr'>" +
                    "<a href='  ' class='SearchUserLink'>" +
                        "<img  src='" + item.Image + "' alt='" + item.Fname + "' class='SearchUserImg'>" +
                        "<span class='searchUserLight'>" +
                              item.Title + ". " + item.Fname +
                              "<span style=' font-weight: bold;color:black'>" + "  " + item.Organization + "</span>" +
                        "</span>" +
                        "<span style='  font-size : 10px; color: #3B5998;'>" + "  " + item.Pubname + "," + item.Pubtype + "</span>" +
                    "</a>"
                    ).appendTo(ul);

            }
        };


    };
    //  var oTable = null;

    jQuery(document).ready(function () {
        App.init();
        PortletDraggable.init();
        FormFileUpload.init();
        EducationalQualification.init();
        WorkExperience.init();
        Supervisions.init();
        Awards.init();
        Publication.init();
        initFields();
    });   

</script>

如果这不起作用,那么您可能需要检查您包含的所有 JavaScript 文件,以确保它们的所有版本都兼容,并且没有两个定义相同的函数/对象。

【讨论】:

  • @JayantBramhankar 你的问题解决了吗?你试过这个解决方案了吗?
【解决方案3】:

而不是添加单个自动完成脚本引用,只需添加 jquery.ui 完整包脚本引用。我遇到了类似的问题,但是在添加 jquery.ui 并删除自动完成脚本后,一切都开始正常工作了。希望对你有帮助

【讨论】:

    猜你喜欢
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2012-10-07
    • 1970-01-01
    • 2015-03-14
    • 2015-08-04
    相关资源
    最近更新 更多