简体   繁体   English

在字符串中执行变量

[英]Execute variable inside a string

I am a ruby on rails developer and have a simple question which I am not able to figure out. 我是Rails开发人员的红宝石,有一个简单的问题,我无法解决。

location.href = "/home?selected_date="+selecteddate;"&days_ago="+dateDiff(selecteddate , todaysdate);

I have to generate a URL by using the above code, here I have a variable called selecteddate which generates a value and dateDiff(selecteddate , todaysdate); 我必须使用上面的代码生成一个URL,这里有一个名为selecteddate的变量,该变量生成一个值和dateDiff(selecteddate,todaysdate); does a difference of the selected date and today's date and gives me a value. 对所选日期和今天日期进行了比较,并给了我一个价值。

Question: When I execute it my URL looks like this: 问题:执行它时,我的URL如下所示:

/home?selected_date=Sun Sep 01 2013 00:00:00 GMT+0530 (IST)

I am missing the &days_ago= part. 我缺少&days_ago =部分。 Any Idea how parse two parameters in the URL. 任何想法如何解析URL中的两个参数。

Thanks you in advance 提前谢谢你

Your syntax is off. 您的语法已关闭。 Plus you should encode it: 另外,您应该对其进行编码:

location.href = "/home?selected_date=" + encodeURIComponent(selecteddate) + "&days_ago=" + encodeURIComponent(dateDiff(selecteddate , todaysdate));

你错过了串联。

location.href = "/home?selected_date="+selecteddate+";&days_ago="+dateDiff(selecteddate , todaysdate);

Try this code: 试试这个代码:

location.href = "/home?selected_date="+selecteddate+"&days_ago="+dateDiff(selecteddate , todaysdate);

The problem is that you're not concatenate the selecteddate string variable. 问题是您没有连接selecteddate字符串变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM