Visual Web Part SharePoint Foundation中的JQuery

问题描述 投票:2回答:4

我无法在sharePoint Foundation中启动这个简单的JQuery函数。

这些是我从http://sharepointdragons.com/2012/04/18/adding-jquery-to-a-visual-web-part-and-then-implement-parentchild-radio-buttons/采取的一些步骤

步骤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>

步骤6:来自document.ready的第一个警报被触发但是buttonClick内的下一个警报不是。

任何帮助将不胜感激

jquery sharepoint web-parts
4个回答
0
投票

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

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

应该

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

0
投票

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

但是这不能在sharepoint Foundation或Sharepoint 2010中使用,因为它们构建在.net 3.5框架上,并且上述功能仅在.NET 4.0中可用。

解决这个问题的方法可能是使用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>

希望这有用


0
投票

尝试更改如下:

代替

$("#tboxJQ")

使用

  $('#' + '<%=this.TextBox1.ClientID  %>')

0
投票

感谢Yahya M. Ammouri,我已经能够使用以下代码调用jQuery方法'alert'。在可视Web部件中,您需要传递元素的clientID,而不是普通网页的元素ID。

下面的代码有助于参考:

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


<div>
<asp:Button ID="btnWow" Text="click" runat="server" />
</div>
© www.soinside.com 2019 - 2024. All rights reserved.