4

I have a datepicker and i want defaultDate attribute, if it will satisfy my condition. Here is a code below,

 $(function () {
                jQuery('#datepicker').datepicker({
                  <% if (parseInt(location.search.indexOf('did=')) >= 0) { %>
                        defaultDate: getDefaultDateVal(),
                   <% } %>
                   //     defaultDate: parseInt(location.search.indexOf('did=')) >= 0 ? getDefaultDateVal() : new Date(),
                        inline: true,
                        maxDate: '+10D',
                        onLoad:function(){alert("hello");},
                        onSelect: function(date) {
                            //alert('Diet plan for ' + date);
                        var d = new Date("January 01, 1970 00:00:00");
                        var d2 = new Date(date);
                        d.setMonth(d.getMonth());
                        var theMonth;
                        theMonth = d.toString();
                        theMonth = theMonth.substr(4, 3);

                        d2.setMonth(d2.getMonth());
                        var theMonth2;
                        theMonth2 = d2.toString();
                        theMonth2 = theMonth2.substr(4, 3);

                        var firstDay = d.getDate();
                        var secondDay = d2.getDate();
                        var firstMonth = theMonth;
                        var secondMonth = theMonth2;
                        var firstYear = d.getFullYear();
                        var secondYear = d2.getFullYear();
                        var firstDate = new Date(firstDay + " " + firstMonth + " " + firstYear);
                        var secondDate = new Date(secondDay + " " + secondMonth + " " + secondYear);
                        var daysDiff = (secondDate.valueOf() - firstDate.valueOf());
                        daysDiff = Math.floor(Math.abs((((daysDiff / 1000) / 60) / 60) / 24));
                        //alert(daysDiff);


                        self.location.href = './Goal.aspx?did=' + daysDiff + '';
                        //filterGetMidHead(date);
                        //wait(500);

                        //   alert(date);
                        var startdate = ((d2.getDate()).toString().length == 1) ? "0" + (d2.getDate()).toString() : (d2.getDate()).toString();
                        var enddate = new Date();
                        enddate.setDate(d2.getDate() + 7);
                        var enddate1 = ((enddate.getDate()).toString().length == 1) ? "0" + (enddate.getDate()).toString() : (enddate.getDate()).toString();
                        //  alert(enddate1 + "hello");
                        var month2 = ((d2.getMonth() + 1).toString().length == 1) ? "0" + (d2.getMonth() + 1).toString() : (d2.getMonth() + 1).toString();
                        var endatemonth = ((enddate.getMonth() + 1).toString().length == 1) ? "0" + (enddate.getMonth() + 1).toString() : (enddate.getMonth() + 1).toString();

                        //alert(new Date().setDate(d2.getDate() + 7));
                        document.getElementById('<%=lblHeaderDate1.ClientID %>').innerHTML = startdate + "/" + month2 + "/" + d2.getFullYear();
                        document.getElementById('<%=lblHeaderDate2.ClientID %>').innerHTML = enddate1 + "/" + endatemonth + "/" + enddate.getFullYear();
                        //    testnew();
                    }
                });

                  <% if (Convert.ToString(Session["LanguageID"])=="IS") 
                       { %>
                    jQuery("#datepicker").datepicker(jQuery.datepicker.regional['is']);
                    <% } %>

                //jQuery("#datepicker").datepicker(jQuery.datepicker.regional['is']);


                //hover states on the static widgets
                jQuery('#dialog_link, ul#icons li').hover(
                        function () { jQuery(this).addClass('ui-state-hover'); },
                        function () { jQuery(this).removeClass('ui-state-hover'); }
                    );
            });

But it is showing me an error. Can anyone help me how to write if condition to set datepicker attribue in $("defaultDate: parseInt(location.search.indexOf('did=')) >= 0 ? getDefaultDateVal() : new Date(),").datepicker?

4

3 回答 3

1

我无法getDefaultDateVal()在您的代码中获得该功能。但希望这将出现在您的代码中的任何地方。所以试试下面的代码:

defaultDate: ((parseInt(location.search.indexOf('did=')) >= 0) ? getDefaultDateVal() : new Date()),
于 2013-06-03T10:04:15.390 回答
1

你想做什么?

<% if (parseInt(location.search.indexOf('did=')) >= 0) { %>
                    defaultDate: getDefaultDateVal(),
<% } %>

parseInt显示javascript但<%显示asp的服务器端代码块

你为什么不只在服务器端做呢?尝试这个

<% if (System.Web.HttpContext.Current.Request.QueryString.ToString().Contains("did=")) { %>
                    defaultDate: getDefaultDateVal(),
<% } %>
于 2013-06-03T10:09:26.540 回答
0

I think, you are writing inline javascript, because you are writing c# code inside javascript, and you are making conditions according to session ID. It's bad practice i think, because your javascript wouldn't be cached and you are mixing client- and server-side code. I suggest to make two different javascript file, and include to the page from c# code into a literal. In this case the If statement can be inside Page_Load, and the javascript inside a separated file.

In this Case the browser can cache it.

example:

Put this tag into aspx:

<asp:Literal runat="server" ID="lscript"/>

c# page load:

if (Session["LanguageID"].ToString()=="IS") 
{
   lscript.Text = "<script src=/js/CondFirst.js></script>";
}
else
{
   lscript.Text = "<script src=/js/CondSecond.js></script>";
}

And you have to register this assembly to use literal:

<%@ Register Assembly="System.Web" Namespace="System.Web.UI" TagPrefix="asp" %>
于 2013-06-03T10:01:40.293 回答