0

我正在尝试在 CEWP 中使用一些自定义 javascript 在选择下拉值时隐藏和显示一些字段。我设法隐藏了文本框(“您的姓名”),但无法隐藏“选择姓名”或“主管姓名”字段 - 这两个是人员选择器。我试图应用在其他地方找到的一些逻辑,但我仍然找不到控件。

这是我的 Javascript 代码:

<script type="text/javascript">

_spBodyOnLoadFunctionNames.push("hideFieldsOnStart");

function hideFieldsOnStart() {

//hide the control at start
var nameID = getTagFromIdentifierAndTitle("input","TextField","Your Name");
var control = getTagFromIdentifierAndTitle("Textarea","UserField_downlevelTextBox","Select Name");
var supername = getTagFromIdentifierAndTitle("Textarea","UserField_downlevelTextBox","Supervisor Name");

//Hide the Course Material field by default

nameID.parentNode.parentNode.parentNode.style.display="none";
control.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.display="none";
//supername.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.display="none";

//add an onchange event to the dropdown
getTagFromIdentifierAndTitle("select","DropDownChoice","Do you have access to Email?").attachEvent("onchange", ChangeEvent);
}

function ChangeEvent()
 {
//get the dropdown
 var dropDown = getTagFromIdentifierAndTitle("select","DropDownChoice","Do you have access to Email?");

//get the selected value
 var option = dropDown.options[dropDown.selectedIndex].text;

//get the control
 var txtName = getTagFromIdentifierAndTitle("Input","TextField","Your Name");
 //var selectSuper = getTagFromIdentifierAndTitle("div","People Picker","Supervisor Name");
 //var selectName = getTagFromIdentifierAndTitle("div","People Picker","Select Name");

//show hide based on your condition
 if(dropDown.options[dropDown.selectedIndex].text == "No")
 {
 //selectSuper.parentNode.parentNode.parentNode.style.display="";
 txtName.parentNode.parentNode.parentNode.style.display="";
 //selectName.parentNode.parentNode.parentNode.style.display="none";
 }
 else
 {
 //selectSuper.parentNode.parentNode.parentNode.style.display="none";
 txtName.parentNode.parentNode.parentNode.style.display="none";
 //selectName.parentNode.parentNode.parentNode.style.display="";
 }

}

//this gets the field based on title identifier and tagname
 function getTagFromIdentifierAndTitle(tagName, identifier, title) {
  var len = identifier.length;
  var tags = document.getElementsByTagName(tagName);
  for (var i=0; i < tags.length; i++) {
  var tempString = tags[i].id;
  if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
   return tags[i];
  }
  }
  return null;
 }

 </script>

这是我的页面的代码:

<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/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/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">
                            <xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>
                        </td>
                        <td class="ms-toolbar" nowrap="nowrap" align="right">
                            <SharePoint:GoBackButton runat="server" ControlMode="New" id="gobackbutton1"/>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td class="ms-toolbar" nowrap="nowrap">
                <SharePoint:FormToolBar runat="server" ControlMode="New"/>
            </td>
        </tr>
        <xsl:call-template name="dvt_1.rowedit">
        <xsl:with-param name="Pos" select="concat('_', position())" />
        </xsl:call-template>

        <tr>
            <td class="ms-toolbar" nowrap="nowrap">
                <table>
                    <tr>
                        <td width="99%" class="ms-toolbar" nowrap="nowrap"><IMG SRC="/_layouts/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">
                            <xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;</xsl:text>
                        </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"><span id="part2"><H3 class="ms-standardheader"><nobr>Do you have access to Email?<span class="ms-formvalidation"> *</span></nobr></H3></span></td>
                        <td width="400px" valign="top" class="ms-formbody"><span id="part8"><SharePoint:FormField runat="server" id="FormField1" ControlMode="New" FieldName="Do_x0020_you_x0020_have_x0020_ac" __designer:bind="{ddwrt:DataBind('i',concat('ff13',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Do_x0020_you_x0020_have_x0020_ac')}" /></span></td>
                    </tr>
                    <tr><td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader"><nobr>Name<span class="ms-formvalidation"> *</span></nobr></H3></td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff2{$Pos}" ControlMode="New" FieldName="Your_x0020_Name" __designer:bind="{ddwrt:DataBind('i',concat('ff2',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Your_x0020_Name')}" /></td>
                    </tr><tr><td width="190px" valign="top" class="ms-formlabel"><span id="part10"><H3 class="ms-standardheader"><nobr>Name<span class="ms-formvalidation"> *</span></nobr></H3></span></td><td width="400px" valign="top" class="ms-formbody"><span id="part11"><span id="part9"><SharePoint:FormField runat="server" id="FormField2" ControlMode="New" FieldName="Select_x0020_Name" __designer:bind="{ddwrt:DataBind('i',concat('ff14',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Select_x0020_Name')}" /></span></span></td></tr><tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>Supervisor&apos;s Name<span class="ms-formvalidation"> *</span>
                                </nobr>
                            </H3>
                        </td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff3{$Pos}" ControlMode="New" FieldName="Supervisor_x0027_s_x0020_Name" __designer:bind="{ddwrt:DataBind('i',concat('ff3',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Supervisor_x0027_s_x0020_Name')}"/><SharePoint:FieldDescription runat="server" id="ff3description{$Pos}" FieldName="Supervisor_x0027_s_x0020_Name" ControlMode="New" /></td>
                    </tr>
                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>Department<span class="ms-formvalidation"> *</span>
                                </nobr>
                            </H3>
                        </td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff4{$Pos}" ControlMode="New" FieldName="NewColumn1" __designer:bind="{ddwrt:DataBind('i',concat('ff4',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@NewColumn1')}"/></td>
                    </tr>
                    <tr><td width="190px" valign="top" class="ms-formlabel"><H3 class="ms-standardheader"><nobr>Incident Option<span class="ms-formvalidation"> *</span></nobr></H3></td><td width="400px" valign="top" class="ms-formbody"><SharePoint:FormField runat="server" id="ff5{$Pos}" ControlMode="New" FieldName="Incident_x0020_Option" __designer:bind="{ddwrt:DataBind('i',concat('ff5',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Incident_x0020_Option')}" /><SharePoint:FieldDescription runat="server" id="ff5description{$Pos}" FieldName="Incident_x0020_Option" ControlMode="New" /></td></tr>
                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>Department Location<span class="ms-formvalidation"> *</span>
                                </nobr>
                            </H3>
                        </td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff6{$Pos}" ControlMode="New" FieldName="NewColumn10" __designer:bind="{ddwrt:DataBind('i',concat('ff6',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@NewColumn10')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff6description{$Pos}" FieldName="NewColumn10" ControlMode="New"/>
                        </td>
                    </tr>

                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>Location of Incident<span class="ms-formvalidation"> *</span></nobr>
                            </H3>
                        </td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff7{$Pos}" ControlMode="New" FieldName="Incident_x0020_Location" __designer:bind="{ddwrt:DataBind('i',concat('ff7',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Incident_x0020_Location')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff7description{$Pos}" FieldName="Incident_x0020_Location" ControlMode="New"/></td>
                    </tr>
                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>Description

                                </nobr>
                            </H3></td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff8{$Pos}" ControlMode="New" FieldName="Description" __designer:bind="{ddwrt:DataBind('i',concat('ff8',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Description')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff8description{$Pos}" FieldName="Description" ControlMode="New"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>Severity<span class="ms-formvalidation"> *</span></nobr>
                            </H3><p class="ms-standardheader">
                            <xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp; </xsl:text>&#160;</p><table><tr><td style="width: 21px; height: 21px"><img alt="" src="../../SiteAssets/Information_icon1.gif" width="20" height="20" /></td><td style="width: 130px"><span id="part5" style="font-weight: normal"><a href="http://intra-gla.clydeunion.local/HSE/Shared Documents/Risk Matrices.xls">Click here f<span style="color: #0072BC">or a guide</span></a></span></td></tr></table></td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff9{$Pos}" ControlMode="New" FieldName="Risk_x0020_Severity" __designer:bind="{ddwrt:DataBind('i',concat('ff9',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Risk_x0020_Severity')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff9description{$Pos}" FieldName="Risk_x0020_Severity" ControlMode="New"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader">
                                <nobr>Likelihood<span class="ms-formvalidation"> *</span>
                                </nobr>
                            </H3><p class="ms-standardheader">
                            <xsl:text xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" ddwrt:nbsp-preserve="yes" disable-output-escaping="yes">&amp;nbsp;
                            </xsl:text>&#160;</p><span id="part6"><table><tr><td style="width: 21px; height: 21px"><img alt="" src="../../SiteAssets/Information_icon1.gif" width="20" height="20" /></td><td style="width: 130px"><span id="part7" style="font-weight: normal"><a href="http://intra-gla.clydeunion.local/HSE/Shared Documents/Risk Matrices.xls">Click here f<span style="color: #0072BC">or a guide</span></a></span></td></tr></table></span></td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff10{$Pos}" ControlMode="New" FieldName="Likelihood" __designer:bind="{ddwrt:DataBind('i',concat('ff10',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Likelihood')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff10description{$Pos}" FieldName="Likelihood" ControlMode="New"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="190px" valign="top" class="ms-formlabel">
                            <H3 class="ms-standardheader"><nobr>Status<span class="ms-formvalidation"> *</span></nobr></H3></td>
                        <td width="400px" valign="top" class="ms-formbody">
                            <SharePoint:FormField runat="server" id="ff11{$Pos}" ControlMode="New" FieldName="Status" __designer:bind="{ddwrt:DataBind('i',concat('ff11',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Status')}"/>
                            <SharePoint:FieldDescription runat="server" id="ff11description{$Pos}" FieldName="Status" ControlMode="New" /></td>
                    </tr><tr id="idAttachmentsRow"><td width="20%" valign="top" class="ms-formlabel" nowrap="true"><SharePoint:FieldLabel ControlMode="New" FieldName="Attachments" runat="server" /></td><td width="80%" valign="top" class="ms-formbody"><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>

如果有人能指出我如何隐藏这两个字段并在选择“是”时显示它们的正确方向,那么我将不胜感激。

谢谢。

4

1 回答 1

0

我看到这是一篇旧帖子,但我认为提供答案可能会很好,因为我相信很多人都有这个问题。

首先,我引用了我的博客,因此有关更多信息,请参见此处 - SharePoint Gypsy - 如何操纵人员选取器控件

Here is the entire code block:

//This runs the function when the form first loads
spBodyOnLoadFunctionNames.push("validatePeoplPicker");

function validatePeoplPicker()
{
var assignedToSubmitter = getSPPeoplePicker("MyPeoplePicker");

//This function finds and returns the proper SharePoint control's HTML
getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i = 0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" 
tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}}
return null;
}

//This function finds the People Picker control's Parent element
function getParentElementByTagName(baseNode, tagName) {
var currNode;
if (baseNode !== null) {
currNode = baseNode.parentNode;
while ((currNode !== null) && (currNode.nodeName != tagName)) {
currNode = currNode.parentNode;
}
return currNode;
}
else {
return null;
}}

//This function finds the People Picker control's Cell element
function getPeoplePickerCell(columnName) {
var search = 'FieldName="' + columnName + '"';
var nodes = document.getElementsByTagName("TEXTAREA");
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].title == "People Picker") {
var outerCell = getParentElementByTagName(nodes[i], "SPAN").parentNode.parentNode;
if (outerCell.innerHTML.indexOf(search) > 0) {
return nodes[i].parentNode;
}}}
return null;
}

//This function returns the People Picker object
function getSPPeoplePicker(columnName) 
{ 
var cell = getPeoplePickerCell(columnName); 
if(cell !== null) 
{ 
return cell.childNodes[0].innerHTML; 
} 
else 
{ 
return null; 
} } }

我希望这会有所帮助!

Tom Molskow SharePoint 吉普赛人

于 2013-03-21T20:11:53.417 回答