2

我无法让这个简单的 JQuery 函数在 sharePoint Foundation 中触发。

这些是我从 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 Farm 解决方案中创建可视 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 内部的下一个警报没有触发。

任何帮助将不胜感激

4

4 回答 4

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>

希望这有帮助

于 2012-10-04T18:30:01.817 回答
0

感谢 Yahya M. Ammouri,我已经能够使用以下代码调用 jQuery 方法“警报”。在可视化 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>
于 2013-06-27T10:56:33.930 回答
0

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

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

应该

$("#tboxJQ").val("Fired using JQuery");
于 2012-10-03T20:31:44.690 回答
0

尝试更改如下:

代替

$("#tboxJQ")

利用

  $('#' + '<%=this.TextBox1.ClientID  %>')
于 2013-03-05T10:56:56.423 回答