PHP和JavaScript日期不匹配 - 奇怪的异常
问题描述:
HTML/PHP:
<button onclick="change_value(<?php echo $date?>)">Go</button>
JavaScript:
function change_value(date_used)
{
alert(date_used);
}
When I right-click on the button and click the "Inspect Element" button shows the parameter correctly, i.e something like this:
<button onclick="change_value(2012-08-22)">Go</button>
But the alert
in the JavaScript is displaying 1981
. That's it - only 1981
. Not only the date is wrong, but the format, too.
What is this happening and how can I fix it?
HTML / PHP: p>
&lt; button onclick =“ change_value(&lt;?php echo $ date?&gt;)“&gt; Go&lt; / button&gt;
code> pre>
JavaScript: p>
function change_value(date_used)
{
alert(date_used);
}
code> pre>
当我右键单击按钮并单击 “Inspect Element”按钮正确显示参数,例如: p>
&lt; button onclick =“change_value(2012-08-22)”&gt; Go&lt; / button&gt ;
code> pre>
但JavaScript中的 alert code>正在显示 1981 code>。 就是这样 - 只有 1981 code>。 不仅日期错误,而且格式也是错误的。 p>
这是怎么回事?我该如何解决? p>
div>
答
Change
<button onclick="change_value(<?php echo $date?>)">Go</button>
to this:
<button onclick="change_value('<?php echo $date?>')">Go</button>
You should pass it as string to JavaScript ...