如何在 Javascript 中编写 PHP 变量

How to write a PHP variable in Javascript?

本文关键字:PHP 变量 Javascript      更新时间:2023-09-26

这是我的JavaScript函数:

    $('.btn-setting').click(function (e) {
    e.preventDefault();
    $('#myModal').modal('show');
});

这就是我在PHP文件中调用它的方式:

   <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">

我的问题是:如何将变量传入:

   $('#myModal<?php $variable ?>').modal('show');

并在PHP文件中调用它,如下所示:

<div class="modal fade" id="myModal<?php echo $variable ;>?" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">

在你的 php 文件中,你需要在你的脚本中 echo 变量,比如

<script>
$('#myModal<?php echo $variable; ?>').modal('show');
</script>

如果您在文件中回显.js

不起作用

更新答案

在文件的 html 部分中php您需要如下所示的回显variable

<div class="modal fade" id="myModal<?=$variable?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

尝试

$('#myModal<?php echo $variable; ?>').modal('show');

使用回显函数

   $('#myModal<?php echo $variable ?>').modal('show');

Php 会给你回显 ( Output one or more strings ) 来表示任务。

$('#myModal<?php echo $variable; ?>').modal('show');