php - 未捕获的类型错误 : Object #<Object> has no method 'autocomplete'

标签 php jquery ajax jquery-ui-autocomplete

我遇到了一个问题,希望大家能帮我解决。

我有一个函数,我想在其中使用自动完成功能。在下面的帮助下,我设法消除了错误,但现在使用 Ajax 时它不起作用。就像我之前写的那样,底部脚本 (inbox.php) 加载到顶部脚本 (home.php) 内。

我发现,如果我单独打开页面(因此只需转到 localhost/inbox.php),自动完成脚本就可以工作,但是当通过 Ajax 时,它会丢失它。这就是为什么我现在认为问题出在 Ajax 脚本中,可以在示例的最后部分找到该脚本。

home.php(主页)

<head>

    <meta charset="UTF-8">
    <link rel="stylesheet" href="homeV2.css" type="text/css" media="screen">
    <script type="text/javascript" src="javascript/index.js"></script>
    <script type='text/javascript' src='http://code.jquery.com/jquery-1.6.4.js'></script>
    <script type='text/javascript' src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.0/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.0/themes/black-tie/jquery-ui.css">

    <title> 
        Homepage Westpop Intranet 
    </title>

    <script>
        window.onload = function()
            {
                showHome(<?=$gid?>);
                refreshChat();
                countdown();                                
            }           
    </script>



</head>

inbox.php(此页面在 home.php 中加载,使用 Ajax)

<script type="text/javascript">
   var names = ['hi', 'bye', 'foo', 'bar'];

    $(document).ready(function() {
        $("#inputNaam").autocomplete({
          source: names
        });
    });
</script>
............ //some other script
    </div>
            </div>
        <div id='profielNieuwBericht'>
            <div id='nieuwBerichtKop'>
                Nieuw bericht
            </div>
            <table>
                <tr>
                    <td>Aan: </td>
                    <td><input type='text' class='inputNieuwBericht' id='inputNaam' /> </td>
                </tr>
                <tr>
                    <td> Onderwerp: </td>
                    <td> <input type='text' class='inputNieuwBericht' id='inputOnderwerp' /></td>
            </table>
            <textarea id='BerichtTextarea'></textarea></br>
            <input type='button' id='BerichtManagementButton' value='Stuur' />
        </div>
    </div>

这就是 Ajax 部分。单击 home.php 中的按钮时将调用此脚本(不包括链接,但通过 onClick='showInbox(id)' 调用)。

function showInbox(id){
if (id==''){
    document.getElementById("middenpag").innerHTML="";
    return;
}

if (window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
}
else{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementById("middenpag").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","inbox.php?id="+id,true);
xmlhttp.send();

}

我希望你们能看到我在这里做错了什么! 谢谢:)

最佳答案

正如评论所指出的,不要包含多个 jQuery 库。选择您想要/需要的一个版本并使用它。您包括 jQuery-ui 版本 1.10.0,它实际上是最新的,only compatible with jQuery core 1.6+ ,因此请删除您的 1.3.0 包含文件。

您正在从 onkeydown 内联事件调用 home.php 中定义的自定义 autocomplete() 方法。这是不必要的,就像您声明的整个 autocomplete() 方法一样。从 basic autocomplete example 出发,您需要执行的唯一代码如下:

配置文件.php html

<input type="text" class="inputNieuwBericht' id="inputNaam' />

在您的示例中,我发现您还错过了输入元素上的结束标记。此外,如果您将其作为表单的一部分提交,则需要填写输入的 name='' 属性,或者处理表单提交的服务器脚本不是无法从请求中获取变量。

javascript 数组声明 - 如果您仅在 profile.php 中使用它,则将其放在那里。如果您在不同的包含文件之间重复使用相同的自动完成选项,则将其放入 home.php 中。或者将其放入一个单独的 .php 文件中,您可以从最终使用它的文件中include_once它。

<script type="text/javascript">
   var names = ['hi', 'bye', 'foo', 'bar'];
</script>

javascript 自动完成绑定(bind) - 每次自动完成只需执行一次,请使用 document.ready 处理程序,并且不要尝试将其与 onkeydown 内联事件一起使用。

<script type="text/javascript">
    $(document).ready(function() {
        $( "#tags" ).autocomplete({
          source: names
        });
    });

你就完成了。最后,正如其他人也提到的那样,不要发布代码的屏幕截图。复制/粘贴您的代码,缩进它以使其格式化为代码,并指出错误发生在哪一行。另外,您应该包含 profile.php 包含的代码位。您对其工作原理的描述不够充分。

The bottom picture (profiel.php) is loaded inside the script in the top picture (home.php).

这可能是一个 iFrame、一个 php include 或 require、任意数量的 javascript/ajax 加载等,所有这些都会对代码需要如何工作产生不同的影响。

关于php - 未捕获的类型错误 : Object #<Object> has no method 'autocomplete' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14540275/

相关文章:

php - 如何将本地时间转换为UTC格式?

PHP - 将两个数组(相同长度)合并为一个关联?

PHP MySQL - 一次更新多行

php - 为什么这段代码不简单地打印字母 A 到 Z?

javascript - jquery if 语句检测到高度

ajax - 您如何使用 Selenium 测试 Ajax 应用程序并使其保持稳定?

javascript - 如何从我的 Eclipse 项目中删除 javascript 验证?

javascript - 将数据加载到使用 JSON 返回的表单字段时出现问题

javascript - 如何使用js立即删除包含字符串(tr_obj)的行

javascript - 在 asp.net 中处理任务时间