我基本上是这样做的,所以当你点击按钮“投票”时
现在我有
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>";
?>
正如您在上面看到的,有一个用于 voteUp() 函数的 onclick。
在我的页面底部我有
<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 时
为什么我在控制台中收到错误消息“ReferenceError:voteUp 未定义”
最佳答案
您已经确定了方法的范围。
相反,您可以将它们附加到窗口以便从任何地方访问。
window.voteUp = function()
{
$.get("voteup.php");
return false;
}
window.voteDown = function()
{
$.get("votedown.php");
return false;
}
关于php - 运行 php 脚本时 Jquery 函数说未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41574289/