gpt4 book ai didi

javascript - 如何解决这个 "Uncaught ReferenceError: $ is not defined"

转载 作者:行者123 更新时间:2023-12-02 23:40:33 24 4
gpt4 key购买 nike

我有一些代码,我需要更新表(MySQL)的一列,调用另一个 php 文件,而不离开某些表可能允许内联编辑的页面。

我在页面的 php 回显中有一个点,可以单击图标来保存输入。此时的代码是:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<?php
$sql = "SELECT * FROM table WHERE a_column='certain_value'";
if (mysqli_query($conn, $sql)) {
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$note = $row["note"];
$code = $row["code"];
}
}
}
// some tabled elements not relevant for the issue
echo "<input type='text' id='note_1' name='note_1' value=$note readonly>";
echo "<input type='text' id='new_note' name='new_note'>";
echo "<img src='icon_to_click.png' id='icon_to_click' name='icon_to_click' >";
?>

<script type="text/javascript">

$(document).ready(function() {
$('#icon_to_click').click(function() {
var note_orig = document.getElementById('note_1').value;
var code_val = '<?php echo "$code" ?>';
var note_new = document.getElementById('new_note').value;
if (note_new != note_orig) {
$.ajax({
type: 'POST',
url: 'update_notes.php',
data: {'code': code_val, 'note': note_new},
success: function(response){
document.getElementById('note_1').value = note_new;
}
});
}
});
});

update_notes.php的相关代码为:

<?php 

// connection

$unsafe_note = $_POST["note"];
$code = $_POST["code"];
require "safetize.php"; // the user input is made safe
$note = $safetized_note; // get the output of safetize.php

$sqlupdate = "UPDATE table SET note='$note' WHERE code='$code'";
if (mysqli_query($conn, $sqlupdate)) {
echo "Note updated";
} else {
echo "Problem in updating";
}

// close connection

?>

现在,当我运行代码并查看该工具时,它会给出错误:Uncaught ReferenceError: $ is not Defined,将错误链接到前面 js 代码的这一行: p>

$(document).ready(function() {

那么,我该如何解决这个问题?

最佳答案

这意味着您尝试在 Javascript 代码中使用 Jquery 而不调用 Jquery 库,或者在库未完全加载的情况下调用代码。

我注意到:

  • 您尚未关闭脚本标记
  • 您使用 Jquery,因此可以使用 $('#id_name') 按 Id 选择元素,而不是 document.getElementById('note_1')
  • 使用 Element.val() 而不是 Element.value 获取元素值

尝试像这样编辑代码

<?php
$sql = "SELECT * FROM table WHERE a_column='certain_value'";
if (mysqli_query($conn, $sql)) {
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$note = $row["note"];
$code = $row["code"];
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Some title</title>
</head>
<body>
<form method="post" accept-charset="UTF-8">
<input type='text' id='note_1' name='note_1' value=<?= $code ?> readonly>";
<input type='text' id='new_note' name='new_note'>";
<img src='icon_to_click.png' id='icon_to_click' name='icon_to_click' >";
</form>
<script>
$(document).ready(function() {
$('#icon_to_click').click(function() {
var note_orig = $('#note_1').val();
var code_val = '<?= $code ?>';
var note_new = $('#new_note').val();
if (note_new != note_orig) {
$.ajax({
type: 'POST',
url: 'update_notes.php',
data: {'code': code_val, 'note': note_new},
success: function(response){
$('#note_1').val() = note_new;
}
});
}
});
});
</script>
</body>
</html>

关于javascript - 如何解决这个 "Uncaught ReferenceError: $ is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56099116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com