Sharepoint:如何通过 Javascript/JQuery 从自定义表单上的 DateTimeField 获取日期值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11743458/
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
Sharepoint: How to get date value from DateTimeField on custom form via Javascript/JQuery?
提问by Bartek Boro
I've searched the web for a while, but I couldn't find a simple answer to my question. So, I've got a Sharepoint List, with Date and Time type, and I created a custom form for adding new element's. On the form, I created a DateTimeField just like that:
我在网上搜索了一段时间,但找不到我的问题的简单答案。所以,我有一个 Sharepoint 列表,带有日期和时间类型,我创建了一个自定义表单来添加新元素。在表单上,我创建了一个 DateTimeField 就像这样:
<SharePoint:DateTimeField runat="server" id="ff_Order_Asset_DeliveryDate" FieldName="DeliveryDate" />
And everything works just fine. Now i would like to add a validation, becasue selected date can't be in the past, so i need to compare it do current date. I would like to do it in javascript using PreSaveAction, but the problem is, i have no clue how to get date value from my control. I've tried something like this:
一切正常。现在我想添加一个验证,因为选择的日期不能是过去的,所以我需要比较它的当前日期。我想使用 PreSaveAction 在 javascript 中完成它,但问题是,我不知道如何从我的控件中获取日期值。我试过这样的事情:
var dateSelected = document.getElementById("<%=ff_Order_Asset_DeliveryDate.ClientID%>");
var dateFromSelection = new Date(dateSelected.value);
var dateNow = new Date();
if (dateFromSelection < dateNow)
doSomething();
But of course it doesn't work. Please enlighten me how could i manage to do that? I will be very gratefull!
但当然它不起作用。请赐教我怎么能做到这一点?我将不胜感激!
------------------------------- ANSWER --------------------------------------
- - - - - - - - - - - - - - - - 回答 - - - - - - - - - --------------------
If someone care: it seemed that only thing i should have done, is use this jQuery syntax:
如果有人关心:似乎我应该做的唯一一件事就是使用以下 jQuery 语法:
var dateSelected = $('input[id*="ff_Order_dFAsset_DeliveryDate"]');
beacuse as Mark Oreta said, Sharepoint added much to ID of my control.
因为正如 Mark Oreta 所说,Sharepoint 为我的控件 ID 增加了很多。
回答by Mark Oreta
Are you opposed to using the the column validation? If so, I suggest just validating that way.
您是否反对使用列验证?如果是这样,我建议只验证这种方式。
If javascript is your only option then it's not going to be as easy as selecting the text box due to the fact that sharepoint adds a bunch of data to the id of the item when it generates the form. Based on what you've posted it looks like you've extracted the form so what I would do is wrap a span around the so that the input is generated inside the div and locate it that way.
如果 javascript 是您唯一的选择,那么它不会像选择文本框那么容易,因为 sharepoint 在生成表单时会向项目的 id 添加一堆数据。根据您发布的内容,您似乎已经提取了表单,因此我要做的是在 周围环绕一个跨度,以便在 div 内生成输入并以这种方式定位。
<span id="span-dateselected">
<SharePoint:DateTimeField runat="server" id="ff_Order_Asset_DeliveryDate" FieldName="DeliveryDate" />
</span>
Then in your javascript you'll need to find the span, then the inputs:
然后在您的 javascript 中,您需要找到跨度,然后是输入:
var dateFromSelection = document.getElementById("span-dateselected").getElementsByTagName("input")[0].value;
The code snippet above makes some assumptions that might or might not be good so you might want to test that the javascript found the input properly. I suggest using Jquery to iterate through the list that's generated, but I realize that might not be an option for you.
上面的代码片段做出了一些可能好也可能不好的假设,因此您可能想要测试 javascript 是否正确找到了输入。我建议使用 Jquery 遍历生成的列表,但我意识到这可能不适合您。
回答by Bartek Boro
If someone care: it seemed that only thing i should have done, is use this jQuery syntax:
如果有人关心:似乎我应该做的唯一一件事就是使用以下 jQuery 语法:
var dateSelected = $('input[id*="ff_Order_dFAsset_DeliveryDate"]');
beacuse as Mark Oreta said, Sharepoint added much to ID of my control.
因为正如 Mark Oreta 所说,Sharepoint 为我的控件 ID 增加了很多。
回答by user2085917
do not use datetime field control see url below: http://www.entwicklungsgedanken.de/2009/08/21/do-not-use-datetimefield-when-displaying-the-date-of-your-field/
不要使用日期时间字段控制见下面的网址:http: //www.entwicklungsgedanken.de/2009/08/21/do-not-use-datetimefield-when-displaying-the-date-of-your-field/
回答by Rainer.R
Have you tried below code?
你有没有试过下面的代码?
var dateSelected = document.getElementById("ff_Order_Asset_DeliveryDate.ClientID");
without the ASp tags?
没有 ASp 标签?