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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19912467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:13:49  来源:igfitidea点击:

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

javascriptphpjquerydreamweaver

提问by leoarce

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.

此代码在运行应用程序时有效,但 Dreamweaver 出现语法错误。它不喜欢那里的问号。我喜欢 DW 没有语法错误。有没有不同的方式来写这个?我有 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 变量所在的新行。

回答by Samuel

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 依赖项,我不建议这样做,但是因为无论如何您都将两者混合...

回答by Jezen Thomas

Looks like you're setting an input value with JavaScript, after having set the .val()method argument with PHP. Why not set the value of the input with PHP directly?

.val()使用 PHP设置方法参数后,您似乎正在使用 JavaScript 设置输入值。为什么不直接用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 dataattribute.

如果您需要在页面加载之外的某个时间运行此脚本,您可以将数据绑定到具有该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'));
});

回答by Adam Azad

I assume that your javascriptisn't in-line (in the same PHPfile) which prevents PHPfrom 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

这是工作示例