最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

I have a SharePoint list with the following single line of text fields: Title, Year and Type or Location. I want to be able to hide the Type or Location table 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.

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 Location field and its value. Here's what I've done so far, but it doesn't work:

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.

I have a SharePoint list with the following single line of text fields: Title, Year and Type or Location. I want to be able to hide the Type or Location table 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.

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 Location field and its value. Here's what I've done so far, but it doesn't work:

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.

Share Improve this question edited Apr 4, 2012 at 11:50 Boris asked Apr 4, 2012 at 11:44 BorisBoris 10.2k35 gold badges112 silver badges148 bronze badges 2
  • This is for DispForm.aspx? If so, are there really input controls on the page? A standard single line of text field would not render within an input field on DispForm.aspx. Also, what version of SharePoint are you working with? – Rich Bennema Commented Apr 4, 2012 at 14:59
  • Yeah, I know that the input... is wrong. I used it because the code is actually taken from another script where the add new item form is modified with jQuery. The SharePoint version is 2010. Thank you for providing the correct answer below. – Boris Commented Apr 5, 2012 at 8:43
Add a comment  | 

6 Answers 6

Reset to default 10

Try:

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

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.

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.

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

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

Try it this way:

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

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.

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.

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.

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

The syntax should be like this:

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

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

It will work.

发布评论

评论列表(0)

  1. 暂无评论