jQuery函数是在运行php脚本时说未定义 [英] Jquery Function is saying not defined when running a php script

查看:99
本文介绍了jQuery函数是在运行php脚本时说未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从本质上讲,我是这么做的,所以当您单击按钮进行投票"时

I am essentially making it so when you click on a button to "vote up"

目前我有

clients.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的votUp()函数.

as you see above there is an onclick for voteUp() function.

我在页面底部

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

scripts.js 中(我从单击div来运行php文件的教程中获得了voteUp()的信息)

and in scripts.js (I got the bit for voteUp() from a tutorial on clicking a div to run a php file)

$(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按钮时

When i click on the Vote Up Button which is the DIV with onclick ="voteUp()"

为什么我在控制台中出现错误,提示"ReferenceError:未定义voteUp"

推荐答案

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

You have scoped out the methods.

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

Instead you can attach them to windows to be accessible from any place.

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

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

这篇关于jQuery函数是在运行php脚本时说未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆