小编典典

jQuery函数是在运行php脚本时说未定义

ajax

我基本上是这样做的,所以当您单击按钮进行“投票”时

目前我有

client.php

<?php

$clientInfo = "SELECT * FROM Clients ORDER BY Client ASC";
$stmt = sqlsrv_query($conn, $clientInfo);

echo "<div style='width: 100%; display: inline-block;'>";

while ($client = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
    echo "<div class='clientid' style='height: 50px; font-size: 18px; vertical-align: middle; display: inline-block; width: 100%'>" .

            "
            <div style='width: 35%;'></div>

            <div onclick=\"voteUp()\" style='display: inline-block; width: 5%;'>      
                <span style='font-size: 20px;' class='hover-cursor fa fa-chevron-up vote-up'></span>
            </div>" .

                 "<div class='hover-cursor hvr-underline-reveal' style='width: 20%; display: inline-block;' data-clientid='{$client['ClientID']}'>" . $client['Client'] . "</div>" .

            "<div onclick=\"voteDown()\" style='display: inline-block; width: 5%;'>
                <span style='font-size: 20px; text-align: right;' class='hover-cursor fa fa-chevron-down vote-down'></span>
            </div>

            <div style='width: 35%;'></div>

         </div> 
        <br />";
}

echo "</div>";

?>

如您在上方看到的,有一个onclick forvotUp()函数。

在页面底部,我有

<script type="text/javascript" src="scripts/scripts.js"></script>

并在 scripts.js中 (我从有关单击div运行php文件的教程中获得了voteUp()的知识)

$(document).ready(function()
{
    $('.pull-me').click(function()
    {
        $('.panel1').slideToggle('fast')
    });
    $('.pull-me').click(function()
    {
        $('.panel2').slideToggle('fast')
    });
    $('.pull-me').click(function()
    {
        $('.panel3').slideToggle('fast')
    });

    $("#selection-box1").change(function()
    {
        document.location.href = $(this).val();
    });

    $(".output").click(function () 
    {
        var noteid = $(this).data("noteid");
        var templatenoteid = $(this).data("templatenoteid");
        var variablenoteid = $(this).data("variablenoteid");

        if ($(this).data("noteid")) 
        {
            $("#right-box").load("noteContent.php", {noteid: noteid});
        }

        else if ($(this).data("templatenoteid")) 
        {
            $("#right-box").load("templateNoteContent.php", {templatenoteid: templatenoteid});
        }

        else
        {
            $("#right-box").load("variableContent.php", {variablenoteid: variablenoteid});
        }
    });


    var modal = document.getElementById('login-box');

    window.onclick = function(event)
    {
        if (event.target == modal) 
        {
            modal.style.display = "none";
        }
    }

    //This script votes up and down for the client

    function voteUp()
    {
        $.get("voteup.php");
        return false;
    }

    function voteDown()
    {
        $.get("votedown.php");
        return false;
    }

});

最后在 voteup.php中

<?php include 'connectionDetails.php'; ?>

<?php

if (isset($_POST['clientid'])) 
{
    $voteup = "UPDATE Clients SET Pos = (SELECT MAX(Pos) FROM Clients) + 1 WHERE ClientID = " . $_POST['clientid'];

    $stmt = sqlsrv_query($conn, $voteup);
}
else
{
    echo "No ClientID";
}

?>

当我单击带onclick =“ voteUp()”的DIV的Vote Up按钮时

为什么在控制台中出现错误,提示“ ReferenceError:未定义表决”


阅读 336

收藏
2020-07-26

共1个答案

小编典典

您已经确定了方法的范围。

相反,您可以将它们附加到Windows,以便可以从任何地方访问。

window.voteUp  = function()
{
    $.get("voteup.php");
    return false;
}

window.voteDown = function()
{
    $.get("votedown.php");
    return false;
}
2020-07-26