簡體   English   中英

在SharePoint 2013中使用JavaScript

[英]Using javascript in sharepoint 2013

我在sharepoint 2013中創建了一個列表,並從該列表中創建了一個表單。我有一個名為formID的字段,生成表單時,會自動為該字段創建一個文本框。 我需要使用JavaScript才能修改該文本框。

以下是為文本框生成的代碼

<SharePoint:FormField runat="server" id="ff1{$Pos}" ControlMode="New" FieldName="Title" __designer:bind="{ddwrt:DataBind('i',concat('ff1',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Title')}"/>

這是HTML。 此HTML也已由sharepoint創建

    <WebPartPages:DataFormWebPart runat="server" EnableOriginalValue="False" DisplayName="commonPage" ViewFlag="8" ViewContentTypeId="" Default="FALSE" ListUrl="" ListDisplayName="" ListName="{FA03B5DA-DA12-404A-A3D7-531AF64C0F93}" ListId="fa03b5da-da12-404a-a3d7-531af64c0f93" PageType="PAGE_NEWFORM" PageSize="-1" UseSQLDataSourcePaging="True" DataSourceID="" ShowWithSampleData="False" AsyncRefresh="False" ManualRefresh="False" AutoRefresh="False" AutoRefreshInterval="60" NoDefaultStyle="TRUE" InitialAsyncDataFetch="False" Title="commonPage" FrameType="None" SuppressWebPartChrome="False" Description="" IsIncluded="True" PartOrder="2" FrameState="Normal" AllowRemove="True" AllowZoneChange="True" AllowMinimize="True" AllowConnect="True" AllowEdit="True" AllowHide="True" IsVisible="True" DetailLink="" HelpLink="" HelpMode="Modeless" Dir="Default" PartImageSmall="" MissingAssembly="Cannot import this Web Part." PartImageLarge="" IsIncludedFilter="" ExportControlledProperties="True" ConnectionID="00000000-0000-0000-0000-000000000000" ID="g_dad58f58_2e43_4881_882e_ab840e0d9b4f" ChromeType="None" ExportMode="All" __MarkupType="vsattributemarkup" __WebPartId="{DAD58F58-2E43-4881-882E-AB840E0D9B4F}" __AllowXSLTEditing="true" WebPart="true" Height="" Width=""><DataSources>
<SharePoint:SPDataSource runat="server" DataSourceMode="ListItem" SelectCommand="&lt;View&gt;&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name=&quot;ContentType&quot;/&gt;&lt;Value Type=&quot;Text&quot;&gt;Item&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;&lt;/View&gt;" UseInternalName="True" UseServerDataFormat="True"><SelectParameters><WebPartPages:DataFormParameter ParameterKey="ListItemId" PropertyName="ParameterValues" DefaultValue="0" Name="ListItemId"></WebPartPages:DataFormParameter><WebPartPages:DataFormParameter ParameterKey="weburl" PropertyName="ParameterValues" DefaultValue="https://tcfddemo.sharepoint.com/sites/Adelphi" Name="weburl"></WebPartPages:DataFormParameter><WebPartPages:DataFormParameter ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="{FA03B5DA-DA12-404A-A3D7-531AF64C0F93}" Name="ListID"></WebPartPages:DataFormParameter>
            </SelectParameters><UpdateParameters><WebPartPages:DataFormParameter ParameterKey="ListItemId" PropertyName="ParameterValues" DefaultValue="0" Name="ListItemId"></WebPartPages:DataFormParameter><WebPartPages:DataFormParameter ParameterKey="weburl" PropertyName="ParameterValues" DefaultValue="https://tcfddemo.sharepoint.com/sites/Adelphi" Name="weburl"></WebPartPages:DataFormParameter><WebPartPages:DataFormParameter ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="{FA03B5DA-DA12-404A-A3D7-531AF64C0F93}" Name="ListID"></WebPartPages:DataFormParameter>
            </UpdateParameters><InsertParameters><WebPartPages:DataFormParameter ParameterKey="ListItemId" PropertyName="ParameterValues" DefaultValue="0" Name="ListItemId"></WebPartPages:DataFormParameter><WebPartPages:DataFormParameter ParameterKey="weburl" PropertyName="ParameterValues" DefaultValue="https://tcfddemo.sharepoint.com/sites/Adelphi" Name="weburl"></WebPartPages:DataFormParameter><WebPartPages:DataFormParameter ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="{FA03B5DA-DA12-404A-A3D7-531AF64C0F93}" Name="ListID"></WebPartPages:DataFormParameter>
            </InsertParameters><DeleteParameters><WebPartPages:DataFormParameter ParameterKey="ListItemId" PropertyName="ParameterValues" DefaultValue="0" Name="ListItemId"></WebPartPages:DataFormParameter><WebPartPages:DataFormParameter ParameterKey="weburl" PropertyName="ParameterValues" DefaultValue="https://tcfddemo.sharepoint.com/sites/Adelphi" Name="weburl"></WebPartPages:DataFormParameter><WebPartPages:DataFormParameter ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="{FA03B5DA-DA12-404A-A3D7-531AF64C0F93}" Name="ListID"></WebPartPages:DataFormParameter>
            </DeleteParameters>
</SharePoint:SPDataSource>
</DataSources>
<Xsl>



<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:dsp="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
    <xsl:output method="html" indent="no"/>
    <xsl:decimal-format NaN=""/>
    <xsl:param name="dvt_apos">&apos;</xsl:param>
    <xsl:param name="ManualRefresh"></xsl:param>
    <xsl:variable name="dvt_1_automode">0</xsl:variable>
    <xsl:template match="/" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:dsp="http://schemas.microsoft.com/sharepoint/dsp" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:SharePoint="Microsoft.SharePoint.WebControls">
        <xsl:choose>
            <xsl:when test="($ManualRefresh = 'True')">
                <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td valign="top">
                            <xsl:call-template name="dvt_1"/>
                        </td>
                        <td width="1%" class="ms-vb" valign="top">
                            <img src="/_layouts/15/images/staticrefresh.gif" id="ManualRefresh" border="0" onclick="javascript: {ddwrt:GenFireServerEvent('__cancel')}" alt="Click here to refresh the dataview."/>
                        </td>
                    </tr>
                </table>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="dvt_1"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="dvt_1">
        <xsl:variable name="dvt_StyleName">ListForm</xsl:variable>
        <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
        <div>
            <span id="part1">
                <table border="0" width="100%">
                    <xsl:call-template name="dvt_1.body">
                        <xsl:with-param name="Rows" select="$Rows"/>
                    </xsl:call-template>
                </table>
            </span>
            <SharePoint:AttachmentUpload runat="server" ControlMode="New"/>
            <SharePoint:ItemHiddenVersion runat="server" ControlMode="New"/>
        </div>
    </xsl:template>
    <xsl:template name="dvt_1.body">
        <xsl:param name="Rows"/>
        <tr>
            <td class="ms-toolbar" nowrap="nowrap">
                <table>
                    <tr>
                        <td width="99%" class="ms-toolbar" nowrap="nowrap"><IMG SRC="/_layouts/15/images/blank.gif" width="1" height="18"/></td>
                        <td class="ms-toolbar" nowrap="nowrap">
                            <SharePoint:SaveButton runat="server" ControlMode="New" id="savebutton1"/>
                        </td>
                        <td class="ms-separator">&#160;</td>
                        <td class="ms-toolbar" nowrap="nowrap" align="right">
                            <SharePoint:GoBackButton runat="server" ControlMode="New" id="gobackbutton1"/>
                        </td>
                        <td><input type="text" id="userType"/></td> <!-- look here -->
                        <td><input type="button" value="click here to change view" id="userTypeButton" onclick="function1()"/></td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td class="ms-toolbar" nowrap="nowrap">
                <SharePoint:FormToolBar runat="server" ControlMode="New"/>
                <SharePoint:ItemValidationFailedMessage runat="server" ControlMode="New"/>
            </td>
        </tr>
        <xsl:call-template name="dvt_1.rowedit"/>
        <tr>
            <td class="ms-toolbar" nowrap="nowrap">
                <table>
                    <tr>
                        <td width="99%" class="ms-toolbar" nowrap="nowrap"><IMG SRC="/_layouts/15/images/blank.gif" width="1" height="18"/></td>
                        <td class="ms-toolbar" nowrap="nowrap">
                            <SharePoint:SaveButton runat="server" ControlMode="New" id="savebutton2"/>
                        </td>
                        <td class="ms-separator">&#160;</td>
                        <td class="ms-toolbar" nowrap="nowrap" align="right">
                            <SharePoint:GoBackButton runat="server" ControlMode="New" id="gobackbutton2"/>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </xsl:template>
    <xsl:template name="dvt_1.rowedit">
        <xsl:param name="Pos" select="position()"/>
        <tr>
            <td>
                <table border="0" cellspacing="0" width="100%">
                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>formID<span class="ms-formvalidation"> *</span>
                                </nobr>
                            </H3>
                        </td>
                        <td width="400px" valign="top" class="ms-formbody" >
                            <SharePoint:FormField runat="server" id="ff1{$Pos}" ControlMode="New" FieldName="Title" __designer:bind="{ddwrt:DataBind('i',concat('ff1',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Title')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff1description{$Pos}" FieldName="Title" ControlMode="New"/>
                        </td>
                    </tr>

                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>surgeon_name</nobr>
                            </H3>
                        </td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff2{$Pos}" ControlMode="New" FieldName="tsnn" __designer:bind="{ddwrt:DataBind('i',concat('ff2',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@tsnn')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff2description{$Pos}" FieldName="tsnn" ControlMode="New"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>surgeon_surname</nobr>
                            </H3>
                        </td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff3{$Pos}" ControlMode="New" FieldName="_x0069_xr2" __designer:bind="{ddwrt:DataBind('i',concat('ff3',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@_x0069_xr2')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff3description{$Pos}" FieldName="_x0069_xr2" ControlMode="New"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>date_created</nobr>
                            </H3>
                        </td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff4{$Pos}" ControlMode="New" FieldName="date_created" __designer:bind="{ddwrt:DataBind('i',concat('ff4',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@date_created')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff4description{$Pos}" FieldName="date_created" ControlMode="New"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>lab_name</nobr>
                            </H3>
                        </td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff5{$Pos}" ControlMode="New" FieldName="d0h5" __designer:bind="{ddwrt:DataBind('i',concat('ff5',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@d0h5')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff5description{$Pos}" FieldName="d0h5" ControlMode="New"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>lab_surname</nobr>
                            </H3>
                        </td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff6{$Pos}" ControlMode="New" FieldName="zn0z" __designer:bind="{ddwrt:DataBind('i',concat('ff6',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@zn0z')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff6description{$Pos}" FieldName="zn0z" ControlMode="New"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>status</nobr>
                            </H3>
                        </td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff7{$Pos}" ControlMode="New" FieldName="status" __designer:bind="{ddwrt:DataBind('i',concat('ff7',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@status')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff7description{$Pos}" FieldName="status" ControlMode="New"/>
                        </td>
                    </tr>
                    <tr id="idAttachmentsRow">
                        <td nowrap="true" valign="top" class="ms-formlabel" width="20%">
                            <SharePoint:FieldLabel ControlMode="New" FieldName="Attachments" runat="server"/>
                        </td>
                        <td valign="top" class="ms-formbody" width="80%">
                            <SharePoint:FormField runat="server" id="AttachmentsField" ControlMode="New" FieldName="Attachments" __designer:bind="{ddwrt:DataBind('i','AttachmentsField','Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Attachments')}"/>
                            <script>
          var elm = document.getElementById(&quot;idAttachmentsTable&quot;);
          if (elm == null || elm.rows.length == 0)
          document.getElementById(&quot;idAttachmentsRow&quot;).style.display=&apos;none&apos;;
        </script>
                        </td>
                    </tr>
                    <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
                        <tr>
                            <td colspan="99" class="ms-vb">
                                <span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" ddwrt:ammode="view"></span>
                            </td>
                        </tr>
                    </xsl:if>
                </table>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>   </Xsl>
<DataFields>
</DataFields>
<ParameterBindings>
         <ParameterBinding Name="ListItemId" Location="QueryString(ID)" DefaultValue="0"/>
         <ParameterBinding Name="weburl" Location="None" DefaultValue="https://tcfddemo.sharepoint.com/sites/Adelphi"/>
         <ParameterBinding Name="ListID" Location="None" DefaultValue="{FA03B5DA-DA12-404A-A3D7-531AF64C0F93}"/>
         <ParameterBinding Name="dvt_apos" Location="Postback;Connection"/>
         <ParameterBinding Name="ManualRefresh" Location="WPProperty[ManualRefresh]"/>
         <ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"/>
         <ParameterBinding Name="Today" Location="CAMLVariable" DefaultValue="CurrentDate"/>
     </ParameterBindings>
</WebPartPages:DataFormWebPart>

這是我寫的JavaScript

<script>
    function function1(){
    var type=document.getElementById(&quot;userType&quot;).value;
        //window.alert(type);
        if(type==&quot;lab&quot;){
            window.alert(&quot;you are lab&quot;);
            document.getElementById(&quot;ff1{$Pos}&quot;).disabled=true;
        }
        else if (type==&quot;surgeon&quot;){
            window.alert(&quot;you are surgeon&quot;);
        }

    }
</script>

我的問題是如何使用“ document.getElementByID”和純javascript訪問此文本框。 我嘗試使用默認ID,但是它不起作用。

總結一下:您可以使用SharePoint生成的ID通過javascript獲取HTML元素。 只需導航至頁面並查看源即可。 從那里獲取ID,並在document.getElementById(xxx)中使用它。

因此,使用'ff1 {$ Pos}'無效,因為頁面加載時會生成/替換元素的ID。

該ID將以ctl00_PlaceHolderMain_ [yourid]開頭

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM