doushou7169 2012-03-28 18:42
浏览 35
已采纳

将php var传递给jQuery函数

How do I pass a variable in a php file that is loaded into a page (DOM) to a jQuery function??

Iv'e tried various method's while searching online but I haven't figured out how to use them correctly.

I need the var navHeaderTitle to be passes to the jQuery load() callback function so it sets the HTML tag, #navHeaderTitle, to the variable called in the php file.

Thnx for you help.

php:

<?php
$con = mysql_connect("localhost","user","pw");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("SELECT * FROM some_list");

$navHeaderTitle = "MY NEW TITLE";//<--I NEED 2 INJECT THIS!!

while($row = mysql_fetch_array($result))
{
echo "<div id='navItem' title='$navHeaderTitle'>";
echo "<h1>" . $row['label'] . "</h1>";
echo "<h2>" . $row['title'] . "</h2>";
echo "<p>" . $row['description'] . "</p>";
echo "</div>";
}
mysql_close($con);
?>

JavaScript in the HTML Head:

<script type="text/javascript">

    var navHeaderTitle = '';

    $(document).ready(  
        function() {
            $("#navContent").load('http://url/my_list.php', function() {
            $('#navHeaderTitle').text($(html).find('div#navItem').attr('title'));//<--GET THE VAR FROM LOADED PHP FILE!!
            }); 
        });
</script>
<body>
<div id="navPanel">

            <div id="navHeader">
                <img src="images/ic_return.png" style="float: left;"/>
                <img  id="listSortBtn" src="images/ic_list_sort.png" style="float: right;"/>
                <h4 id="navHeaderTitle"></h4>//THIS IS WHAT NEEDS THE VAR DATA!!
            </div>

            <div id="navScrollContainer" class="navContentPosition">
                <div id="navContent">HTML CONTENT from PHP GETS DUMPED IN HERE</div>
            </div>

        </div>
</body>

Ive tried using this but not sure how to:

$.get('scripts/my_list.php', {}, function(data){ 
                   data = split(':'); 
                })
  • 写回答

3条回答 默认 最新

  • dongyuan3094 2012-03-28 18:45
    关注

    replace echo("$navHeaderTitle"); with

    echo("<script> var navHeaderTitle = $navHeaderTitle </script>");
    

    and remove var navHeaderTitle = ''; from the <head> script..

    that will setup a JS variable like you're using, but you have to do that before the code in the <head> loads...

    EDIT

    ok don't echo("$navHeaderTitle"); you can put it into the HTML like:

    echo "<div id='navItem' title='$navHeaderTitle'>";
    

    then in the JS you can do:

    <script type="text/javascript">
        var navHeaderTitle = '';
    
        $(document).ready(  
            function() {
                $("#navContent").load('http://url/my_list.php', function(response) {
                    $('#navHeaderTitle').text($(response).attr('title'));
                }); 
            });
    </script>
    

    here's a jsfiddle demo: http://jsfiddle.net/JKirchartz/hdBzF/ (it's using fiddle's /echo/html/ so the load has some extra stuff to emulate the ajax)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?