如何根据另一个字段中的值设置 SharePoint 列表字段中的默认值? [英] How can I set the default value in a SharePoint list field, based on the value in another field?
问题描述
我在 SharePoint(特别是 MOSS 2007)中有一个自定义列表.一个字段是一个是/否复选框,标题为有任何缺陷吗?"另一个字段是关闭者"并命名关闭工单的人.
I have a custom list in SharePoint (specifically, MOSS 2007.) One field is a yes/no checkbox titled "Any defects?" Another field is "Closed by" and names the person who has closed the ticket.
如果没有缺陷,那么我希望自动关闭票证.如果有,则稍后应填写Closed by"字段.
If there are no defects then I want the ticket to be auto-closed. If there are, then the "Closed by" field ought to be filled in later on.
我想我可以像这样为Closed by"设置一个计算出的默认值:
I figured I could set a calculated default value for "Closed by" like this:
=IF([Any defects?],"",[Me])
但 SharePoint 抱怨我引用了一个字段.我想这是有道理的;当新列表项首次打开以供输入且任何字段中尚无值时,将触发默认值.
but SharePoint complains I have referenced a field. I suppose this makes sense; the default values fire when the new list item is first opened for entry and there are no values in any fields yet.
我知道可以根据列值创建计算字段,但在这种情况下,以后无法编辑该字段.
I understand it is possible to make a calculated field based on a column value but in that case the field cannot be edited later.
有人对如何实现我想要做的事情有任何建议吗?
Does anyone have any advice how to achieve what I am trying to do?
是否可以有一个OnSubmit"类型的事件,允许我在保存列表项时执行一些代码?
Is it possible to have a "OnSubmit" type event that allows me to execute some code at the point the list item is saved?
谢谢.
推荐答案
在页面中包含一个内容编辑器 Web 部件 (newform.aspx/editform.aspx) 并使用 jQuery(或只是普通的 javascript)来处理默认设置价值.
Include a content editor web part in the page (newform.aspx / editform.aspx) and use jQuery (or just plain javascript) to handle the setting of default values.
一些示例代码:
在列表 newform.aspx 中,包含对 jquery 的引用.如果您查看 html 代码,您可以看到每个输入标签根据字段的 GUID 获取一个 id,以及一个设置为字段显示名称的标题.
In the lists newform.aspx, include a reference to jquery. If you look at the html code, you can see that each input tag gets an id based on the field's GUID, and a title that's set to the fields display name.
现在,使用 jquery,我们可以使用 jQuery 选择器获取这些字段,如下所示:
now, using jquery we can get at these fields using the jQuery selector like this:
按标题:
$("input[title='DISPLAYNAMEOFFIELD']");
通过 id(如果您知道字段的内部 guid,破折号将被下划线替换:
by id (if you know the field's internal guid, the dashes will ahve to be replaced by underscores:
// example field id, notice the guid and the underscores in the guid ctl00_m_g_054db6a0_0028_412d_bdc1_f2522ac3922e_ctl00_ctl04_ctl15_ctl00_ctl00_ctl04_ctl00_ctl00_TextField
$("input[id*='GUID']"); //this will get all input elements of which the id contains the specified GUID, i.e. 1 element
我们将其封装在 jQuery 的 ready()
函数中,因此所有调用只会在文档完全加载时进行:
We wrap this in the ready()
function of jQuery, so all calls will only be made when the document has fully loaded:
$(document).ready(function(){
// enter code here, will be executed immediately after page has been loaded
});
通过结合这两个,我们现在可以将下拉列表的 onchange
事件设置为以下内容
By combining these 2 we could now set your dropdown's onchange
event to the following
$(document).ready(function(){
$("input[title='DISPLAYNAMEOFFIELD']").change(function()
{
//do something to other field here
});
});
这篇关于如何根据另一个字段中的值设置 SharePoint 列表字段中的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!