Uncaught SyntaxError:意外的标识符

时间:2015-01-12 01:28:21

标签: javascript

<link type='text/css' rel='stylesheet' href='./css/style.css'>
<link type='text/css' rel='stylesheet' href='css/dataTable.css'>

<?php

    include("config.php");
    include("meniu.php");
    include("meniuV.php");
    echo "<script type='text/javascript' charset='utf8' src='//code.jquery.com/jquery-1.10.2.min.js'></script>";
    echo "<script type='text/javascript' charset='utf8' src='//cdn.datatables.net/1.10.4/js/jquery.dataTables.js'></script>";

    session_start();

?>
<div id="bilet" class="BiletCSS">

</div>
<?php

    $sql="SELECT * FROM meciuri WHERE data='2015-01-10'";
    $resursa=mysql_query($sql);

    if(!$resursa)
    {
        exit(mysql_error());
    }
    if(mysql_num_rows($resursa)==0)
    {
        print 'Nu s-a gasit niciun rezultat!<br>'; 
    }
    echo "<div class='ziua1image'>";
    echo "<img src='./images/fotbal2.jpg' style='width:600px;height:300px'>";
    echo "</div>";

    echo "<div class='div'>";
    echo "<table  id='tabelMeciuri' class='display' cellspacing='0'>";
    echo "<thead>";
    echo "<tr class='data_cota'>";
    echo "<td>Uefa Champions League</td>";
    echo "<td>1</td>";
    echo "<td>X</td>";
    echo "<td>2</td>";
    echo "<td>1x</td>";
    echo "<td>x2</td>";

    echo "</tr>";
    echo "</thead>";
    echo "<tbody>";

    $resursa=mysql_query($sql);

    while($row = mysql_fetch_array($resursa))
    {
        $idConta1=$row['id_meci']."_1";
        $idConta2=$row['id_meci']."_2";
        $idConta3=$row['id_meci']."_3";
        $idConta4=$row['id_meci']."_4";
        $idConta5=$row['id_meci']."_5";
        $meci1=$row['nume_meci'];
            print' <tr class="ooddd">
                    <td>' .$row['nume_meci'] . '</td> 
                    <td><label id="'.$idConta1.'" onclick="getCota('.$row['id_meci'].','.$meci1.',1)" >' .$row['victorie_1'] . '</label></td>
                    <td><label id="'.$idConta2.'" onclick="getCota('.$row['id_meci'].',2)" >' .$row['egal_x'] . '</td>
                    <td><label id="'.$idConta3.'" onclick="getCota('.$row['id_meci'].',3)" >' .$row['victorie_2'] .'</td>
                    <td><label id="'.$idConta4.'" onclick="getCota('.$row['id_meci'].',4)" >' .$row['victorie_egal_1x'] .'</td>
                    <td><label id="'.$idConta5.'" onclick="getCota('.$row['id_meci'].',5)" >' .$row['victorie_egal_x2'] .'</td>
                </tr>';
    }
    echo "</tbody>";
    echo "</table>";
    echo "</div>";
?>


<script>
function getCota(id,numeMeci,index){
debugger;
    var idCota="#"+id+"_"+index;
    var idCota2=id+"_"+index;
    $("#bilet").append("<div id='optiune'><label>"+$(numeMeci).text()+"</label> <label>"+$(idCota).text()+"</label> </div>");
}
$(document).ready(function() {
    $('#tabelMeciuri').DataTable();
} );
</script>

<style>
.BiletCSS{
    width: 350pxpx;
    height: 400px;
    background-color:#989898;
    float: right;
    margin-right: 30px;
}
</style>

我想从数据库中选择一个名称和一个数字,并将它们放在表格旁边的单独div中。 当我尝试只选择数字('。$ ['egal_x']。')时,它可以工作,但如果我想添加名称('。$ ['victorie_1']。')我什么都得不到在那个div中,它向我显示错误...... 有人能告诉我如何解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

如果要将PHP结果插入JavaScript字符串,则必须构建字符串。

更改

<td><label id="'.$idConta1.'" onclick="getCota('.$row['id_meci'].','.$meci1.',1)" >' .$row['victorie_1'] . '</label></td>

<td><label id="'.$idConta1.'" onclick="getCota(\''.$row['id_meci'].'\',\''.$meci1.'\',1)" >' .$row['victorie_1'] . '</label></td>

否则其结果将作为名称传递:

 // your code:
 onclick="getCota(Shakhtar ,meci,1)"

 // after change
onclick="getCota('Shakhtar' ,'meci',1)"

答案 1 :(得分:-1)

我不确定这是否是您的问题,但您没有在最后四列中指定numeMeci参数。例如:

onclick="getCota('.$row['id_meci'].',2)" > // no second parameter

如果缺少该信息,您的getCota(id,numeMeci,index)将收到numeMeci中的索引值,而不是index参数。实际上,该功能将尝试类似:

$(2).text()

jQuery可能不会那样。