简体   繁体   English

如何将动态生成的div id传递给ajax?

[英]How to pass dynamically generated div id to ajax?

Hey i've got some problem. 嘿,我有一些问题。

My website is divided into two columns. 我的网站分为两栏。 On the left is sidebar which contains list of users dynamically generated from database, on the right-hand side should be unique chart generated by javascript framework (ajax) based on user_id. 左边是侧边栏,其中包含从数据库动态生成的用户列表,右侧应该是基于user_id的javascript框架(ajax)生成的唯一图表。 And this chart should be shown after choosing some user from list. 从列表中选择一些用户后,应显示此图表。 The php file live-data.php which is used by this javascript/ajax needs GET parameter. 这个javascript / ajax使用的php文件live-data.php需要GET参数。 Now it's: 现在是:

url: "php/live-data.php" url:“php / live-data.php”

and

$.get("php/live-data.php?Consultar=1", function(UltimosDatos) $ .get(“php / live-data.php?Consultar = 1”,函数(UltimosDatos)

but it should be 但它应该是

url: "php/live-data.php?user_id=2" url:“php / live-data.php?user_id = 2”

and

$.get("php/live-data.php?user_id=2&Consultar=1", function(UltimosDatos) $ .get(“php / live-data.php?user_id = 2&Consultar = 1”,函数(UltimosDatos)

Where 2 is user_id got after clicking some user name from dynamically generated list. 从动态生成的列表中单击某个用户名后,其中2为user_id。 The php script live-data.php is ready for GET variable and returns proper json for chart framwork (this javascript shown below). php脚本live-data.php已准备好用于GET变量,并为图表框架返回正确的json(此javascript如下所示)。 I dont know how to pass div id to this ajax code. 我不知道如何将div id传递给这个ajax代码。

HTML+PHP: HTML + PHP:

<div id="left" class="pre-scrollable col-lg-3">
    <div class="list-group">
        <?php include("php/dbSettings.php");
        $result = $conn->query("SELECT * FROM user ORDER BY user_id");
        if (!$result) {
            die(mysqli_error($conn));
        }
        while ($user = mysqli_fetch_array($result)){
        echo '<a href="#'.$user['user_id'].'" data-toggle="tab" class="list-group-item">' . $user['firstName'] . " " .$user['lastName'] . '</a>';
        }
        ?>
    </div>
</div>

<div id="right" class="col-lg-9">
    <div class="tab-content">
        <?php include( "php/dbSettings.php"); 
        $result=$ conn->query("SELECT * FROM users ORDER BY user_id"); 
        if (!$result) { 
        die(mysqli_error($conn));
        } 
        while ($user = mysqli_fetch_array($result)){
        echo '<div class="tab-pane" id="'.$user['user_id'].'">
        <div id="chart" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
        </div>';
        } ?>
    </div>
</div>

Javascript/Ajax: 的JavaScript / AJAX:

<script>
    $(function() {
        $(document).ready(function() {
            var ultimox;
            var ultimoy;

            $.ajax({
                url: "php/live-data.php", //i want this line to be "php/live-data.php?user_id=2" and 2 is variable got from user list onlick
                type: 'get',
                success: function(DatosRecuperados) {
                    $.each(DatosRecuperados, function(i, o) {
                        //some deleted code - unimportant
                    });

                    //some deleted code - unimportant

                    $('#chart').highcharts({
                        //draws chart
                    });

                }
            });
        });
        setInterval(function() {
                $.get("php/live-data.php?Consultar=1", function(UltimosDatos) { //i want this line to be "php/live-data.php?php/live-data.php?Consultar=1&user_id=2" and 2 is variable got from user list onlick
                        //updates chart
                    }
                });
        }, 1000);

        //some deleted code - unimportant

    });
</script>

I hope someone can help me on my way. 我希望有人可以帮助我。

Thanks, Paul 谢谢,保罗

It looks like the hash will be set to the user id when the anchor is clicked, based on this <a href="#'.$user['user_id'].'" so you could read the hash value and pass it as data on the request. 根据此<a href="#'.$user['user_id'].'"看起来哈希将被设置为用户ID,因此您可以读取哈希值并将其传递为请求中的数据。 Either this: 要么:

 $.ajax({ url: "php/live-data.php?user_id=" + window.location.hash.substr(1), type: 'get', success: function(DatosRecuperados) { // ... } }); 

or this: 或这个:

 var dataObj = {}; dataObj['user_id'] = window.location.hash.substr(1); //create a data object to pass on query string, set user id value $.ajax({ url: "php/live-data.php", type: 'get', data: dataObj, //pass object with request success: function(DatosRecuperados) { // ... } }); 

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

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