jquery - 在加载和更改时在 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); });
解决方案
您可以尝试像这样更改代码:
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); });
推荐阅读
- mysql - Mysql '大于(>)' 查询总是返回 0
- windows-installer - 将环境变量传递给 msi 中的 INSTALLDIR
- python - 重新启动实时绘图时,pyqtgraph实时绘图中断
- c++ - c++ 类和对象中的 this 指针
- node.js - 无法在 MAC OSX 10.12.6 上通过 npm 安装 firebase-tools(错误:在 '...x","firebase":"~1.0.1 附近解析时 JSON 输入意外结束)
- ubuntu - 如何将波斯字体添加到 Next.js?
- python - 转换为字符串未标记的孩子 美丽的汤
- javascript - 删除 href attr 并添加 action="1004"
- java - 如何制作带有授权版的产品
- php - 如何使用 Jquery 将值附加到选项标签