VisualWebPart SharePoint基金会中的jQuery 我无法使这个简单的jQuery函数在SharePoint基金会中被火。

VisualWebPart SharePoint基金会中的jQuery 我无法使这个简单的jQuery函数在SharePoint基金会中被火。,jquery,sharepoint,web-parts,Jquery,Sharepoint,Web Parts,以下是我遵循的一些步骤 步骤1:将jquery-1.4.1.js文件添加到SiteAssets 步骤2:在Web场解决方案中创建可视化Web部件 步骤3:添加以下代码 步骤4:在服务器上部署web部件(由于非jquery功能正在工作,因此成功) 步骤5:但是jquery功能不起作用 以下是web部件的所有代码 WebPart1.ascx <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> &l

以下是我遵循的一些步骤

步骤1:将jquery-1.4.1.js文件添加到SiteAssets


步骤2:在Web场解决方案中创建可视化Web部件


步骤3:添加以下代码


步骤4:在服务器上部署web部件(由于非jquery功能正在工作,因此成功)


步骤5:但是jquery功能不起作用

以下是web部件的所有代码

WebPart1.ascx

    <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
  <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral,    PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SanctuaryVisualWebPartUserControl.ascx.cs" Inherits="SanctuaryVisualWebPart.SanctuaryVisualWebPart.SanctuaryVisualWebPartUserControl" %>
<%@ Register assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.WebControls" tagprefix="asp" %>
<SharePoint:ScriptLink ID="ScriptLink1" runat="server"  name="~sitecollection/SiteAssets/jquery-1.4.1.js "  />


  <style type="text/css">
       .style1
    {
        width: 100%;
        height: 247px;
    }
    .style2
    {
        width: 398px;
    }
    .style3
    {
        width: 421px;
    }
</style>
       <script type="text/javascript">
           $(document).ready(function () {

              alert("Fired using JQuery Document ready");
               $("#btnJQ").click(function () {

                   $("#tboxJQ").val("Fired using JQuery");
                  alert("Fired using JQuery");
               });
           });
              </script>

<table bgcolor="#CCFFCC" border="3" cellpadding="3" cellspacing="3" 
    class="style1">




    <tr>
    <td>
        <asp:Button ID="btnJQ" runat="server" Text="JQuery" Width="110px" />
        </td>
    <td>
        <asp:TextBox ID="tboxJQ" runat="server"></asp:TextBox>
        </td>
    </tr>
</table>

.style1
{
宽度:100%;
高度:247px;
}
.style2
{
宽度:398px;
}
.style3
{
宽度:421px;
}
$(文档).ready(函数(){
警报(“使用JQuery文档准备就绪触发”);
$(“#btnJQ”)。单击(函数(){
$(“#tboxJQ”).val(“使用JQuery激发”);
警报(“使用JQuery激发”);
});
});
步骤6:document.ready中的第一个警报已触发,但按钮单击中的下一个警报未触发


任何帮助都将不胜感激

您的tboxJQ选择器错误-如果您是通过id引用元素,则需要在其前面加上哈希。所以

$("tboxJQ").val("Fired using JQuery");
应该是

$("#tboxJQ").val("Fired using JQuery");

tboxJQ的选择器是错误的-如果您通过id引用元素,则需要在其前面加一个哈希。所以

$("tboxJQ").val("Fired using JQuery");
应该是

$("#tboxJQ").val("Fired using JQuery");

在做了一些研究之后,我发现上面访问元素id的方法在sharepoint中不起作用,因为当web部件嵌入sharepoint母版页时,id会发生变化。唯一可行的方法是设置clientdmode=“Static”,在这种情况下jquery将能够访问元素

但不能在SharePoint基金会或SharePoint 2010中使用,因为它们是在.NET 3.5框架上构建的,而上述能力仅在.NET 4中可用。

解决这个问题的一个办法是使用html元素

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="JqueryWebPartUserControl.ascx.cs" Inherits="JqueryWebPart.JqueryWebPart.JqueryWebPartUserControl" %>
<SharePoint:ScriptLink ID="ScriptLink1" runat="server"  name="~sitecollection/SiteAssets/jquery-1.4.1.js"/>

 <script type="text/javascript">
     ExecuteOrDelayUntilScriptLoaded(
   function () {
       $(document).ready(function () {

           $('#tboxJQ').val("Fired using JQuery Ready");

           $('#btnJQ').click(function () {

               $('#tboxJQ').val("Fired using JQuery");

           });
       });
   }, "sp.js");
              </script>


<table>
<tr><td></td></tr>
<tr>
<td>
    <input id="btnJQ" type="button" value="button" /></td>

</tr>
<tr>
<td>
    <input id="tboxJQ" type="text" /></td>

</tr>
</table>

ExecuteOrderLayUntilscript已加载(
函数(){
$(文档).ready(函数(){
$('#tboxJQ').val(“使用jqueryready启动”);
$('#btnJQ')。单击(函数(){
$('#tboxJQ').val(“使用JQuery激发”);
});
});
},“sp.js”);

希望这有帮助

在做了一些研究之后,我发现上面访问元素id的方法在sharepoint中不起作用,因为当web部件嵌入sharepoint母版页时,id会发生变化。唯一可行的方法是设置clientdmode=“Static”,在这种情况下jquery将能够访问元素

但不能在SharePoint基金会或SharePoint 2010中使用,因为它们是在.NET 3.5框架上构建的,而上述能力仅在.NET 4中可用。

解决这个问题的一个办法是使用html元素

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="JqueryWebPartUserControl.ascx.cs" Inherits="JqueryWebPart.JqueryWebPart.JqueryWebPartUserControl" %>
<SharePoint:ScriptLink ID="ScriptLink1" runat="server"  name="~sitecollection/SiteAssets/jquery-1.4.1.js"/>

 <script type="text/javascript">
     ExecuteOrDelayUntilScriptLoaded(
   function () {
       $(document).ready(function () {

           $('#tboxJQ').val("Fired using JQuery Ready");

           $('#btnJQ').click(function () {

               $('#tboxJQ').val("Fired using JQuery");

           });
       });
   }, "sp.js");
              </script>


<table>
<tr><td></td></tr>
<tr>
<td>
    <input id="btnJQ" type="button" value="button" /></td>

</tr>
<tr>
<td>
    <input id="tboxJQ" type="text" /></td>

</tr>
</table>

ExecuteOrderLayUntilscript已加载(
函数(){
$(文档).ready(函数(){
$('#tboxJQ').val(“使用jqueryready启动”);
$('#btnJQ')。单击(函数(){
$('#tboxJQ').val(“使用JQuery激发”);
});
});
},“sp.js”);

希望这是有帮助的

尝试以下更改:

而不是

$("#tboxJQ")
使用

$('#'+'')

尝试按以下方式进行更改:

而不是

$("#tboxJQ")
使用

$('#'+'')

多亏了Yahya M.Ammouri,我能够用下面的代码调用jQuery方法“alert”。在可视web部件中,您需要传递元素的clientID,而不需要传递元素id,就像传递普通web页面一样

以下代码可供参考:

<script type="text/javascript">
    $(function () {
        $('#' + '<%=this.btnWow.ClientID  %>').click(function () {
            alert("Hello world!");
        });
    });    
</script>


<div>
<asp:Button ID="btnWow" Text="click" runat="server" />
</div>

$(函数(){
$('#'+'')。单击(函数(){
警报(“你好,世界!”);
});
});    

多亏了Yahya M.Ammouri,我能够用下面的代码调用jQuery方法“alert”。在可视web部件中,您需要传递元素的clientID,而不需要传递元素id,就像传递普通web页面一样

以下代码可供参考:

<script type="text/javascript">
    $(function () {
        $('#' + '<%=this.btnWow.ClientID  %>').click(function () {
            alert("Hello world!");
        });
    });    
</script>


<div>
<asp:Button ID="btnWow" Text="click" runat="server" />
</div>

$(函数(){
$('#'+'')。单击(函数(){
警报(“你好,世界!”);
});
});    

我的道歉,那是个打字错误。我已经更正了,但是Jquery仍然没有启动。我很抱歉,这是一个输入错误。我已经更正了它,但是Jquery仍然没有启动。