首页 > 解决方案 > 在加载和更改时在 SharePoint 中运行 jquery 函数

问题描述

我一直在使用以下 jquery 在页面加载时隐藏日期字段,然后在另一个字段的值更改为“否”时显示它。如何更新脚本,而不是使用更改事件并等待其他字段的值更改,而是根据最初与页面一起加载的其他字段的值显示/隐藏日期字段,并隐藏/显示如果其他字段的值然后更改(换句话说,如果我打开页面并且它已经设置为否,它默认显示日期字段,但如果我将其更改为是则隐藏它;相反,如果我打开页面并且它已经设置为是,默认情况下它会隐藏日期字段,如果我将其更改为否,则会显示它)。我知道我不能在这里使用 on('load' 事件代替 on('change',但不确定我能做什么。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://**MASKED**"></script>
<script>
 $(document).ready(function () 
{ // Get a the choice field 
var choiceField = SPUtility.GetSPField('PrimaryField');
// Hide the target fields in form load
SPUtility.GetSPField('Date Field').Hide();
// create a function to show or hide a field based on the selected choice Field value 
var ShowHideField = function() { 
var selectedFieldValue = choiceField.GetValue(); 
// Hide the Date Field field if the selected value is not 'Yes' 
if(selectedFieldValue != 'Yes') { 
SPUtility.GetSPField('Date Field').Hide(); } 
else { SPUtility.GetSPField('Date Field').Show(); } }; 
// attach the function to choice field 
$(choiceField.Dropdown).on('change', ShowHideField); });

标签: jquerysharepointhideshow

解决方案


您可以尝试像这样更改代码:

var choiceField = SPUtility.GetSPField('PrimaryField');
// Show/Hide the target fields in form load
if(choiceField.GetValue()!= 'Yes'){
SPUtility.GetSPField('Date Field').Hide();
}
// create a function to show or hide a field based on the selected choice Field value 
var ShowHideField = function() { 
var selectedFieldValue = choiceField.GetValue(); 
// Hide the Date Field field if the selected value is not 'Yes' 
if(selectedFieldValue != 'Yes') { 
SPUtility.GetSPField('Date Field').Hide(); } 
else { SPUtility.GetSPField('Date Field').Show(); } }; 
// attach the function to choice field 
$(choiceField.Dropdown).on('change', ShowHideField); });

推荐阅读