如何在jQuery/JavaScript中正确插入PHP变量?
此代码在运行应用程序时有效,但是Dreamweaver给出了语法错误.它不喜欢那里的问号.我希望DW不会出现语法错误.有另一种写方法吗?我有DW cs5.5,我无法升级Dreamweaver版本.
This code works when running the application, but Dreamweaver is giving a syntax error. It doesn't like the question mark there. I like DW to be syntax error free. Is there a different way to write this? I have DW cs5.5 I can't upgrade Dreamweaver version.
if ( $('#postage6').val() == "Your Permit Standard" ) {
$('#postage6rate').val('<?php echo $your_permit_standard; ?>');
}
在问号前加上反斜杠只会使它像这样打印,这是不正确的.
Putting a backslash before the question mark just makes it print like this, which is not right.
if ( $('#postage6').val() == "Your Permit Standard" ) {
$('#postage6rate').val('<\?php echo $your_permit_standard; ?>');
}
当渲染时,应该有一个像这样的值:
when it renders, there is supposed to be a value like this:
if ( $('#postage6').val() == "Your Permit Standard" ) {
$('#postage6rate').val('0.333');
}
这也不起作用:
if ( $('#postage6').val() == "Your Permit Standard" ) {
var somevar = "<?php echo $your_permit_standard; ?>";
$('#postage6rate').val(somevar);
}
语法错误只是从PHP变量所在的行转移到PHP变量所在的新行.
The syntax error just transfers from the line where the PHP variable was to the new line where the PHP variable is.
您可以在单独的php块中定义值:
You could define the value in a separate php block:
<script type="text/javascript">
var value = '<?=$your_permit_standard?>';
</script>
然后在您的JS中使用它:
And then use it in your JS:
if ( $('#postage6').val() == "Your Permit Standard" ) {
$('#postage6rate').val(value);
}
但是随后您将在PHP中引入JS依赖关系,我不建议这样做,但是由于您仍然将两者混合在一起...
But then you would be introducing JS dependency in PHP, which I wouldn't recommend, but since you're mixing both anyway...