【问题标题】:Run jquery function in SharePoint on load AND change在加载和更改时在 SharePoint 中运行 jquery 函数
【发布时间】:2021-11-14 11:38:03
【问题描述】:

我一直在使用以下 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); });

【问题讨论】:

  • 欢迎来到 Stack Overflow。您似乎正在使用.Hide(),其语法为.hide()。它们区分大小写,必须正确使用。
  • 谢谢@Twisty,但是当我将其更改为小写时,我的代码停止工作。过去它在大写字母 H 上运行良好。我现在只想将其从仅在用户选择不同值时隐藏字段到在表单加载时(以及更改时)已选择该值时隐藏字段。
  • 请正确缩进您的代码以帮助他人帮助您。
  • 如果我知道我会怎么做。我对 jscript 的了解仅限于复制/粘贴其他人创建的内容并根据需要对其进行逆向工程。我自己也不知道怎么写。 :(

标签: jquery sharepoint hide show


【解决方案1】:

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

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); });

【讨论】:

  • 感谢您的想法。我确实尝试过,但不幸的是它似乎没有用。事实上,无论加载或更改什么值,日期字段都没有隐藏。
猜你喜欢
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 1970-01-01
相关资源
最近更新 更多