我无法运行Javascript代码

时间:2022-06-17 16:27:49

Edit the problem is on the bottom!!

编辑问题在底部!!

I want to get my autocomplete function to work and always I get this :

我想让我的自动完成功能工作,我总是得到这个:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>). Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

无法修改Controls集合,因为控件包含代码块(即<%...%>)。描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

My script and the html code:

我的脚本和HTML代码:

<head runat="server">
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <title><%--<%= Page.Title %> --%>- Ticketsystem</title>
  <link href="~/Content/master.css" rel="stylesheet" type="text/css" />
  <asp:PlaceHolder runat="server">
    <%: Scripts.Render( "~/bundles/modernizr") %>
  </asp:PlaceHolder>

  <webopt:bundlereference runat="server" path="~/Content/css" />
  <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />



   <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.js"></script>


<script type="text/javascript" lang="ja">
    $(function ()
    {
       $('#tbCompany').autocomplete(
           {
               source: function (request, response)
               {
               $.ajax({
                   url: "Autocomlete.asmx/GetCompanyNames",
                   data: "{ 'searchTerm': '" + request.term + "' }",
                   type: "POST",
                   dataType: "json",
                   contentType: "application/json;charset=utf-8",
                   success: function (result) {
                       response(result.d);
                   },
                   error: function (result) {
                       alert('There is a problem processing your request');
                   }
               });
           },
           minLength: 0
       });
   });
</script>
</asp:PlaceHolder>

  
</head>

<body>
  <form runat="server">
<asp:TextBox ID="tbCompany" runat="server" Style="margin-top:10px" ClientIDMode="static"></asp:TextBox>

I don´t know if my script is wrong or somthing is missing. I tried to put it on the end of my page, in a div or placeholder like suggested from other questions.

我不知道我的剧本是错的还是缺少了什么。我试图把它放在我的页面末尾,在div或placeholder中,就像其他问题所建议的那样。

edit: Now i did nearly everything what you suggested and now it looks like above and the error below.

编辑:现在我做了几乎所有你建议的东西,现在它看起来像上面和下面的错误。

error:

Uncaught TypeError: $(...).autocomplete is not a function

未捕获的TypeError:$(...)。autocomplete不是一个函数

7 个解决方案

#1


1  

As you can see here: https://support.microsoft.com/en-us/kb/976112 the inline expression <%: doesn't exist.

正如您在此处所见:https://support.microsoft.com/en-us/kb/976112内联表达式<%:不存在。

Try to change <%: ... %> to <%= ... %>.

尝试将<%:...%>更改为<%= ...%>。

Your also loading jquery twice. Your jquery UI version is 1.10.3 but you're loading version jquery-1.9.1.js.

你也加载jquery两次。您的jquery UI版本是1.10.3但是您正在加载版本jquery-1.9.1.js。

Try to load these versions:

尝试加载这些版本:

<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css">

#2


1  

You need to place your script inside placeholder tag.

您需要将脚本放在占位符标记内。

 <asp:PlaceHolder runat="server">
<script type="text/javascript" lang="ja">
    $(function() {
      $("#<%=tbCompany.ClientID%>").autocomplete({
        source: function(request, response) {
          $.ajax({
            url: "Autocomlete.asmx/GetCompanyNames",
            data: "{ 'searchTerm': '" + request.term + "' }",
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            success: function(result) {
              response(result.d);
            },
            error: function(result) {
              alert('There is a problem processing your request');
            }
          });
        },
        minLength: 0
      });
    });
  </script>
</asp:PlaceHolder>

EDIT: It seems that latest error is because of missing jQuery UI js or incorrect sequence. Can you remove all the jQuery related js files and add the latest version of jQuery afterthat jQuery UI in your code ?

编辑:似乎最新的错误是因为缺少jQuery UI js或错误的序列。你可以删除所有jQuery相关的js文件,并在你的代码中添加jQuery UI后的最新版本的jQuery吗?

#3


0  

it seems too many jQuery files added in the head, try to use only one jquery and it may resolve this issue

似乎在头部添加了太多jQuery文件,尝试只使用一个jquery,它可以解决这个问题

you can just use only the latest version minified file.

你只能使用最新版本的缩小文件。

#4


0  

Are you loading Jquery-ui.{version}.js as i can't see it in your code that is for autocomplete error.

您是否正在加载Jquery-ui。{version} .js,因为我无法在您的代码中看到自动填充错误。

If you are writing code in seprate js file then <% %> will not work , In Place of that you can use ClientIDMode as static and in place aspx syntax you can use

如果你在seprate js文件中编写代码,那么<%%>将无法工作,代替你可以使用ClientIDMode作为静态和就地使用aspx语法你可以使用

$("#tbCompany")

Hope it will work.

希望它会奏效。

#5


0  

Try <%# tbCompany.ClientID %> instead <%=tbCompany.ClientID%>

尝试<%#tbCompany.ClientID%>而不是<%= tbCompany.ClientID%>

<script type="text/javascript" lang="ja">
    $(function() {
      $("#<%# tbCompany.ClientID %>").autocomplete({
        source: function(request, response) {
          $.ajax({
            url: "Autocomlete.asmx/GetCompanyNames",
            data: "{ 'searchTerm': '" + request.term + "' }",
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            success: function(result) {
              response(result.d);
            },
            error: function(result) {
              alert('There is a problem processing your request');
            }
          });
        },
        minLength: 0
      });
    });
  </script>

#6


0  

Did you mind try separated the "#<%=tbCompany.ClientID%>" use variable? As: <%= String value = tbCompany.ClientID%> And :$("#value").... May the "autocomplete" is not the point.

您是否介意尝试分离“#<%= tbCompany.ClientID%>”使用变量?如:<%= String value = tbCompany.ClientID%>并且:$(“#value”)....可能“自动完成”不是重点。

#7


0  

As in the comments, you're using .net 4.5.

与评论中一样,您使用的是.net 4.5。

.net 4 bought with it some changes to the asp.net controls. Especially one attribute called ClientIDMode

.net 4随便买了一些asp.net控件的变化。特别是一个名为ClientIDMode的属性

This allows you to control how ASP.Net renders and renames the ID of controls. You can now tell .net to leave them alone..

这允许您控制ASP.Net如何呈现和重命名控件的ID。你现在可以告诉.net让他们独自一人..

You can change this:

你可以改变这个:

$('#<%=tbCompany.ClientID%>').autocomplete({...

To this:

$('#tbCompany').autocomplete({...

And on the element with the ID of tbCompany add the attribute ClientIDMode="static" - this will tell .net to leave the ID alone and use whatever you have set.

在ID为tbCompany的元素上添加属性ClientIDMode =“static” - 这将告诉.net单独保留ID并使用您设置的任何内容。

See more about ClientIDMode here

在此处查看有关ClientIDMode的更多信息

#1


1  

As you can see here: https://support.microsoft.com/en-us/kb/976112 the inline expression <%: doesn't exist.

正如您在此处所见:https://support.microsoft.com/en-us/kb/976112内联表达式<%:不存在。

Try to change <%: ... %> to <%= ... %>.

尝试将<%:...%>更改为<%= ...%>。

Your also loading jquery twice. Your jquery UI version is 1.10.3 but you're loading version jquery-1.9.1.js.

你也加载jquery两次。您的jquery UI版本是1.10.3但是您正在加载版本jquery-1.9.1.js。

Try to load these versions:

尝试加载这些版本:

<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.11.4/themes/ui-lightness/jquery-ui.css">

#2


1  

You need to place your script inside placeholder tag.

您需要将脚本放在占位符标记内。

 <asp:PlaceHolder runat="server">
<script type="text/javascript" lang="ja">
    $(function() {
      $("#<%=tbCompany.ClientID%>").autocomplete({
        source: function(request, response) {
          $.ajax({
            url: "Autocomlete.asmx/GetCompanyNames",
            data: "{ 'searchTerm': '" + request.term + "' }",
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            success: function(result) {
              response(result.d);
            },
            error: function(result) {
              alert('There is a problem processing your request');
            }
          });
        },
        minLength: 0
      });
    });
  </script>
</asp:PlaceHolder>

EDIT: It seems that latest error is because of missing jQuery UI js or incorrect sequence. Can you remove all the jQuery related js files and add the latest version of jQuery afterthat jQuery UI in your code ?

编辑:似乎最新的错误是因为缺少jQuery UI js或错误的序列。你可以删除所有jQuery相关的js文件,并在你的代码中添加jQuery UI后的最新版本的jQuery吗?

#3


0  

it seems too many jQuery files added in the head, try to use only one jquery and it may resolve this issue

似乎在头部添加了太多jQuery文件,尝试只使用一个jquery,它可以解决这个问题

you can just use only the latest version minified file.

你只能使用最新版本的缩小文件。

#4


0  

Are you loading Jquery-ui.{version}.js as i can't see it in your code that is for autocomplete error.

您是否正在加载Jquery-ui。{version} .js,因为我无法在您的代码中看到自动填充错误。

If you are writing code in seprate js file then <% %> will not work , In Place of that you can use ClientIDMode as static and in place aspx syntax you can use

如果你在seprate js文件中编写代码,那么<%%>将无法工作,代替你可以使用ClientIDMode作为静态和就地使用aspx语法你可以使用

$("#tbCompany")

Hope it will work.

希望它会奏效。

#5


0  

Try <%# tbCompany.ClientID %> instead <%=tbCompany.ClientID%>

尝试<%#tbCompany.ClientID%>而不是<%= tbCompany.ClientID%>

<script type="text/javascript" lang="ja">
    $(function() {
      $("#<%# tbCompany.ClientID %>").autocomplete({
        source: function(request, response) {
          $.ajax({
            url: "Autocomlete.asmx/GetCompanyNames",
            data: "{ 'searchTerm': '" + request.term + "' }",
            type: "POST",
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            success: function(result) {
              response(result.d);
            },
            error: function(result) {
              alert('There is a problem processing your request');
            }
          });
        },
        minLength: 0
      });
    });
  </script>

#6


0  

Did you mind try separated the "#<%=tbCompany.ClientID%>" use variable? As: <%= String value = tbCompany.ClientID%> And :$("#value").... May the "autocomplete" is not the point.

您是否介意尝试分离“#<%= tbCompany.ClientID%>”使用变量?如:<%= String value = tbCompany.ClientID%>并且:$(“#value”)....可能“自动完成”不是重点。

#7


0  

As in the comments, you're using .net 4.5.

与评论中一样,您使用的是.net 4.5。

.net 4 bought with it some changes to the asp.net controls. Especially one attribute called ClientIDMode

.net 4随便买了一些asp.net控件的变化。特别是一个名为ClientIDMode的属性

This allows you to control how ASP.Net renders and renames the ID of controls. You can now tell .net to leave them alone..

这允许您控制ASP.Net如何呈现和重命名控件的ID。你现在可以告诉.net让他们独自一人..

You can change this:

你可以改变这个:

$('#<%=tbCompany.ClientID%>').autocomplete({...

To this:

$('#tbCompany').autocomplete({...

And on the element with the ID of tbCompany add the attribute ClientIDMode="static" - this will tell .net to leave the ID alone and use whatever you have set.

在ID为tbCompany的元素上添加属性ClientIDMode =“static” - 这将告诉.net单独保留ID并使用您设置的任何内容。

See more about ClientIDMode here

在此处查看有关ClientIDMode的更多信息