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条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名