Javascript 如何根据字段名称 (jQuery) 在 SharePoint 显示表单中隐藏字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10010405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:28:42  来源:igfitidea点击:

How to hide a field in SharePoint Display Form based on the field name (jQuery)?

javascriptjquerysharepoint

提问by Boris

I have a SharePoint list with the following single line of text fields: Title, Yearand Type or Location. I want to be able to hide the Type or Locationtable row in the default display form. I know that I should create a JavaScript script and put it in Content Editor web part inside DispForm.aspx.

我有一个 SharePoint 列表,其中包含以下单行文本字段:Title,YearType or Location. 我希望能够Type or Location在默认显示表单中隐藏表格行。我知道我应该创建一个 JavaScript 脚本并将它放在 DispForm.aspx 内的内容编辑器 Web 部件中。

I am not fluent with jQuery syntax, thus I need help with the code, i.e. I don't know how to reference the table row which contains Type or Locationfield and its value. Here's what I've done so far, but it doesn't work:

我不熟悉 jQuery 语法,因此我需要代码方面的帮助,即我不知道如何引用包含Type or Location字段及其值的表行。这是我到目前为止所做的,但它不起作用:

jQuery(document).ready(function($) {
   $("input[title='Type or Location']").closest("tr").hide();
});

I know that the "input[title='Type or Location']"part is incorrect; at least I think it's that. Could anyone help me out? Thank you.

我知道那"input[title='Type or Location']"部分是不正确的;至少我认为是这样。有人可以帮我吗?谢谢你。

回答by Rich Bennema

Try:

尝试:

jQuery(document).ready(function($) {
   $("h3.ms-standardheader:contains('Type or Location')").closest("tr").hide();
});

回答by Christophe

I am not sure why you want to use jQuery for that. In SharePoint, you can choose to make a field required, optional or hidden. In most cases, just switching to hidden will address your issue.

我不确定您为什么要为此使用 jQuery。在 SharePoint 中,您可以选择将字段设为必填、可选或隐藏。在大多数情况下,只需切换到隐藏即可解决您的问题。

For the record, I would also try to avoid as much as possible the use of jQuery(document).ready, it might conflict with the SharePoint out of the box onload event. In your case it is not needed.

作为记录,我也会尽量避免使用 jQuery(document).ready,它可能与 SharePoint 开箱即用的 onload 事件发生冲突。在你的情况下,它是不需要的。

Update: here is a way to do this with jQuery:

更新:这是一种使用 jQuery 执行此操作的方法:

$("td.ms-formlabel:contains('Type or Location')").parent().hide();

回答by Kishan

The syntax should be like this:

语法应该是这样的:

$("input[title='Type']").closest("tr").hide();

$("input[title='Location']").closest("tr").hide();

It will work.

它会起作用。

回答by AR Ebhendra Pagoti

If its just the Default Display Form, How about just creating a view and making it default?

如果它只是默认显示表单,那么创建一个视图并将其设为默认值怎么样?

回答by Chris Gessler

Try it this way:

试试这个方法:

jQuery(document).ready(function($) {
   $("input[title='Type'],input[title='Location']").closest("tr").hide();
});

回答by Marc D Anderson

It depends what type of column Type ior Location is. If it's a Single line of text, then you're close. You should use a DOM inspector like IE's Developer Tools or Firebug to see what the actual title of the input element is.

这取决于 Type ior Location 是什么类型的列。如果它是单行文本,那么您就差不多了。您应该使用 DOM 检查器(如 IE 的开发人员工具或 Firebug)来查看输入元素的实际标题是什么。

If the column is a different type, then it's likely not an input element. Using the DOM inspector again, you can look at what elements make up the field control and decide on your selector from that.

如果该列的类型不同,则它可能不是输入元素。再次使用 DOM 检查器,您可以查看组成字段控件的元素并从中决定您的选择器。

Finally, remember that hiding things in script is not secure. A savvy user can turn off the script or otherwise change the script so that they can edit it. It all depends on your requirements.

最后,记住在脚本中隐藏东西是不安全的。精明的用户可以关闭脚本或以其他方式更改脚本,以便他们可以对其进行编辑。这一切都取决于您的要求。

// UPDATE // Ah, you said DispForm. As was pointed out in another answer, there aren't any input elements in a DispForm. You need to correct your selector.

// 更新 // 啊,你说的是 DispForm。正如在另一个答案中指出的那样,DispForm 中没有任何输入元素。您需要更正您的选择器。