简体   繁体   English

如何在jQuery / JavaScript中正确插入PHP变量?

[英]How do I insert PHP variable inside jQuery/JavaScript properly?

This code works when running the application, but Dreamweaver is giving a syntax error. 该代码在运行应用程序时有效,但是Dreamweaver给出了语法错误。 It doesn't like the question mark there. 它不喜欢那里的问号。 I like DW to be syntax error free. 我喜欢DW无语法错误。 Is there a different way to write this? 有另一种写方法吗? I have DW cs5.5 I can't upgrade Dreamweaver version. 我有DW cs5.5,我无法升级Dreamweaver版本。

    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');
    }

Also this doesn't work: 而且这不起作用:

    if ( $('#postage6').val() == "Your Permit Standard" ) {
        var somevar = "<?php echo $your_permit_standard; ?>";
        $('#postage6rate').val(somevar);
    }

The syntax error just transfers from the line where the PHP variable was to the new line where the PHP variable is. 语法错误只是从PHP变量所在的行转移到PHP变量所在的新行。

You could define the value in a separate php block: 您可以在单独的php块中定义值:

<script type="text/javascript">
   var value = '<?=$your_permit_standard?>';
</script>

And then use it in your JS: 然后在您的JS中使用它:

if ( $('#postage6').val() == "Your Permit Standard" ) {
    $('#postage6rate').val(value);
}

But then you would be introducing JS dependency in PHP, which I wouldn't recommend, but since you're mixing both anyway... 但是随后您将在PHP中引入JS依赖关系,我不建议这样做,但是由于您仍然将两者混合在一起...

Looks like you're setting an input value with JavaScript, after having set the .val() method argument with PHP. 在用PHP设置.val()方法参数之后,您似乎正在使用JavaScript设置输入值。 Why not set the value of the input with PHP directly? 为什么不直接使用PHP设置输入的值?

<input type="text" name="postage6rate" value="<?php echo $your_permit_standard; ?>">

If you need to run this script at some time other than page load, you could bind the data to an element with the data attribute. 如果您需要在页面加载以外的某个时间运行此脚本,则可以将数据绑定到具有data属性的元素。

<input type="text" name="postage6rate" data-permit="<?php echo $your_permit_standard; ?>">

And then when you need to run your script… 然后,当您需要运行脚本时...

window.addEventListener('onSomeEvent', function addTheData() {
  var $input = $('input[name="postage6rate"]');
  $input.val($input.data('permit'));
});

I assume that your javascript isn't in-line (in the same PHP file) which prevents PHP from executing 我认为您的javascript不是内联的(在同一PHP文件中),这会阻止PHP执行

Try: 尝试:

<?php 
$your_permit_standard = "0.335";
?>
<html>
  <body>
    <input id="postage6" name="postage6" value="Your Permit Standard"/>
    <input id="postage6rate" name="postage6rate"/>
    <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
    <script>
    if ( $('#postage6').val() == "Your Permit Standard" ) {
        $('#postage6rate').val('<?php echo $your_permit_standard; ?>');
    }
    </script>
  </body>
<html>

And here's the working example 这是工作示例

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

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