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

Sharepoint: How to get date value from DateTimeField on custom form via JavascriptJQuery? - Stack Overflow

programmeradmin2浏览0评论

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 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 pare 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:

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:

var dateSelected = $('input[id*="ff_Order_dFAsset_DeliveryDate"]');

beacuse as Mark Oreta said, Sharepoint added much to ID of my control.

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 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 pare 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:

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:

var dateSelected = $('input[id*="ff_Order_dFAsset_DeliveryDate"]');

beacuse as Mark Oreta said, Sharepoint added much to ID of my control.

Share Improve this question edited Aug 20, 2012 at 15:01 Bartek Boro asked Jul 31, 2012 at 15:20 Bartek BoroBartek Boro 1651 gold badge4 silver badges11 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 1

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.

<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:

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.

If someone care: it seemed that only thing i should have done, is use this jQuery syntax:

var dateSelected = $('input[id*="ff_Order_dFAsset_DeliveryDate"]');

beacuse as Mark Oreta said, Sharepoint added much to ID of my control.

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/

Have you tried below code?

var dateSelected = document.getElementById("ff_Order_Asset_DeliveryDate.ClientID");

without the ASp tags?

发布评论

评论列表(0)

  1. 暂无评论