-2

我想知道如何根据点击事件获取价值。我有获取文本值的代码。

document.getElementById(elem).innerText;

其中 elem 是特定的 id 。我想将其分配给特定变量并将其用作从数据库中查询的字符串。但问题是我无法将其保存到变量中。

window.hold = document.getElementById(elem).innerText;

我这样做是为了将它分配给一个变量。但是我应该将它分配给一个 PHP 变量,而那部分给了我一个错误。请有人帮助我。

所以基本上我想要的是:

<td id='1'>one</td>
<td id='2'>two</td>
<td id='3'>three</td>

所以如果我点击一个。我应该捕获 td 内的数据并为其分配一个变量。提前致谢。

<?php

echo "<div id='one'>";
echo "<div align='center' id='title' style='font-size:32px;font-family: 'trebuchet MS', sans-serif;'>Meter Health</div>";
echo "<table class='table' width='100%' cellpadding='0' cellspacing='5' style='font-size:32px;'>";
$id = 0;
for ($i = 0; $i < 10; $i++) {
    $id++;
    echo "<tr>";
    for ($a = 0; $a < 10; $a++) {
        $c = rand(2000, 9000);
        $b = rand(10, 100);
        if ($b <= 40) {
            $color = 'red';
        } elseif ($b <= 60) {
            $color = 'orange';
        } else {
            $color = 'green';
        }
        echo "<td id=$id; title='$b%'; bgcolor='$color';
                    onmouseover='ChangeColor(this, true);' 
                      onmouseout='ChangeColor(this, false);'             
                    align ='center'
                    onclick='view(); print($b);'


            >$c</td>";
    }
    echo "</tr>";
}
echo "</table>";
echo "</div>";

?>

PHP 将生成表,我想要一个函数来获取分配给变量的值作为基于点击事件的字符串。

<html>
    <head>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <!--[if lt IE 9]><script language="javascript" type="text/javascript" src="dist/excanvas.js"></script><![endif]-->

<script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.jqplot.min.js"></script>
<link rel="stylesheet" type="text/css" href="js/jquery.jqplot.css" />
<script  language="javascript" type="text/javascript" src="js/jqplot.barRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jqplot.categoryAxisRenderer.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jqplot.pointLabels.min.js"></script>


    <script>

        function ChangeColor(tableRow, highLight) {
            if(highLight) {
                tableRow.style.backgroundColor = 'yellow';
            } else {
                tableRow.style.backgroundColor = defaultStatus;
            }
        }

        $(function () {
            $(document).tooltip();
        });


        function view() {
            document.getElementById("one").style.visibility = "visible";
            if(document.getElementById("one").style.display == "none") {
                document.getElementById("one").style.display = "";
                document.getElementById("two").style.display = "none";
            } else {
                document.getElementById("one").style.display = "none";
                document.getElementById("two").style.display = "";
                $.jqplot.config.enablePlugins = true;
                var s1 = [2, 6, 7];
                var ticks = ['IP', 'BILLING', 'LOAD SURVEY'];

                plot1 = $.jqplot('chart1', [s1], {
                    // Only animate if we're not using excanvas (not in IE 7 or IE 8)..
                    animate: !$.jqplot.use_excanvas,
                    seriesDefaults: {
                        renderer: $.jqplot.BarRenderer,
                        pointLabels: {
                            show: true
                        }
                    },
                    axes: {
                        xaxis: {
                            renderer: $.jqplot.CategoryAxisRenderer,
                            ticks: ticks
                        }
                    },
                    highlighter: {
                        show: true
                    }
                });

                $('#chart1').bind('jqplotDataClick',
                    function (ev, seriesIndex, pointIndex, data) {
                        $('#info1').html('series: ' + seriesIndex + ', point: ' + pointIndex + ', data: ' + data);
                    }
                );
            }
        }


    </script>
    <script>    
        $(document).ready(function(){

        });
    </script>
    <style>
        table td
{
    border:1px solid #ccc;
    padding:4px;

    width: 45px;
    height: 45px;
    display: inline-block;
    overflow: auto;
}


        td{

            -moz-border-radius-topleft: 75px;
-moz-border-radius-topright:0px;
-moz-border-radius-bottomleft:0px;
-moz-border-radius-bottomright:75px;
-webkit-border-top-left-radius:75px;
-webkit-border-top-right-radius:0px;
-webkit-border-bottom-left-radius:0px;
-webkit-border-bottom-right-radius:75px;
border-top-left-radius:75px;
border-top-right-radius:0px;
border-bottom-left-radius:0px;
border-bottom-right-radius:75px

        }

    </style>

    </head>

    <body>

        <?php
        echo "<div id='one'>";
        echo  "<div align='center' id='title' style='font-size:32px;font-family: 'trebuchet MS', sans-serif;'>Meter Health</div>";
        echo "<table class='table' width='100%' cellpadding='0' cellspacing='5' style='font-size:32px;'>";
            $id=0;
        for($i=0;$i<10;$i++)
         {  $id++;

            echo "<tr>";
            for($a=0;$a<10;$a++){
                $c=rand(2000,9000);
                $b=rand(10,100);

                 if($b<=40){
                    $color='red';
                }
                elseif($b<=60){
                    $color='orange';
                }
                else{
                    $color='green';
                }



            echo "<td id=$id; title='$b%'; bgcolor='$color';
                    onmouseover='ChangeColor(this, true);' 
                      onmouseout='ChangeColor(this, false);'             
                    align ='center'
                    onclick='view(); print($id);'


            >$c</td>";


            }
            echo "</tr>";



         }
         echo"</table>";
         echo "</div>";




        ?>
        <div id='two'  style='display:none;'>
       <div id="chart1" style="height:400px;width:700px;  margin-left: 9em; margin-top: 3em; margin-bottom: 4em; text-align: center;"></div>
       <div><button onclick='view();'>Back</button></div>
        </div>
      <script>
        function print(elem) {
            // window.hold=document.getElementById(elem).innerHTML;
            window.hold = document.getElementById(elem).innerText;
            document.write(window.hold);
            console.log(hold);
            // console.log(elem);
            // document.getElementById("two").innerHTML=elem;
        }
      </script>  
    </body>  
</html>
4

3 回答 3

0
var hold = document.getElementById(elem).innerText;
于 2013-08-19T03:17:29.203 回答
0

创建一个对象,例如:

    GLOBAL = { "variable" : "" }

然后对于点击事件:

    tdClicked = function(){
      GLOBAL.variable = document.getElementByID(elem).innerText;
    }
于 2013-08-19T03:32:40.663 回答
0

只需更改print($id)print(this)

function print(elem) {
    var hold = elem.innerText;
    console.log(hold);
}
于 2013-08-19T03:35:44.843 回答