简体   繁体   English

ASP.NET <form runat=server> 打破jQuery

[英]ASP.NET <form runat=server> breaks jQuery

In a nutshell, it seems that when an ASP.NET page has the <form runat=server> tag, it's breaking some jQuery scripts. 简而言之,似乎当ASP.NET页具有<form runat=server>标记时,它将破坏一些jQuery脚本。 Here's an example: 这是一个例子:

I have a very simple page that only has a checkbox, like this: 我有一个非常简单的页面,只有一个复选框,如下所示:

<input type="checkbox" id="myCheckbox01" />

and a call to a jQuery function like this: 和对这样的jQuery函数的调用:

<script language=javascript>
    $(document).ready(function () {
        $("[type=checkbox]").iphoneStyle();
    });
</script>

Please, note that I'm not referencing any IDs specifically. 请注意,我没有专门引用任何ID。 The jQuery call is for all checkboxes found in the page. jQuery调用适用于页面中找到的所有复选框。

This is working fine, and the jQuery call works as expected. 这工作正常,并且jQuery调用按预期方式工作。 But... 但...

When I enclose the checkbox in a ASP.NET form, like below: 当我将复选框包含在ASP.NET表单中时,如下所示:

<form runat="server" id="form1">
    <input type="checkbox" id="myCheckbox01" />
</form>

The jQuery call stops working, and I get the JavaScript error "Object doesn't support property or method 'iphoneStyle'" . jQuery调用停止工作,并且出现JavaScript错误“对象不支持属性或方法'iphoneStyle'”

Again, please note that I'm not referencing any specific IDs, nor is the checkbox an ASP.NET (runat=server) control -- it's just a plain old checkbox. 同样,请注意,我没有引用任何特定的ID,该复选框也不是ASP.NET(runat = server)控件,它只是一个普通的旧复选框。

I can't seem to track this problem down. 我似乎无法追踪这个问题。 Any help will be greatly appreciated. 任何帮助将不胜感激。

I had this problem. 我有这个问题。 jQuery itself would work but any plugin I added would not. jQuery本身可以使用,但是我添加的任何插件都无法使用。 I kept getting "Object doesn't support property or method..." complaints. 我不断收到“对象不支持属性或方法...”的抱怨。 The resolution was to move the references to the jQuery plugins from the Site.Master aspx page to the "main" page where I was using the jQuery plugin. 解决方案是将对jQuery插件的引用从Site.Master aspx页面移动到我正在使用jQuery插件的“主”页面。 I thought I was being clever by putting all my script references in the Site.Master but I was too clever afterall. 我以为我将所有脚本引用都放在Site.Master中变得很聪明,但是毕竟我太聪明了。

Change your script like this. 像这样更改您的脚本。 it works for me 这个对我有用

  <script language="javascript">
      $(document).ready(function () {
      $('input:checkbox').iphoneStyle();
      });
  </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM