Javascript 如何解决此“未捕获引用错误:$未定义”
我有一些代码,我需要更新MySQL表的一列,调用另一个php文件,而不离开某些表允许内联编辑的页面 我在页面的php回音中有一点,点击图标可以保存输入。此时的代码是:Javascript 如何解决此“未捕获引用错误:$未定义”,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一些代码,我需要更新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_val
<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;
}
});
}
});
});
那么,我该如何解决这个问题呢?嘿,我前一天也遇到过同样的错误,这是因为您没有使用所需的jquery库脚本。请尝试使用一些更新的Jquery CDN:这肯定会有帮助 或
在任何jquery插件文件之前包含jquery.js文件。这意味着您试图在Javascript代码中使用jquery而不调用jquery库,或者在库未完全加载的情况下调用代码 我注意到: 您尚未关闭脚本标记 您可以使用Jquery,这样就可以使用$'id\u name'按id而不是文档选择元素。getElementById'note\u 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>
尝试按Ctrl+F5组合键并在控制台选项卡中查看错误。它看起来完全像是在加载jquery之前运行的代码。尝试更改文档上的document.ready代码。addEventListener'DOMContentLoaded',event=>{//event occurrent}是否确实在HTML中包含jQuery?@Anis R.:从代码中可以看出,@Dmytro-Huz:你能帮我把它说得更程序化一点吗?我在jquery-include调用之前发现一个包含的文件中有一个输入错误,这就是jquery不能正确运行的原因。但尽管如此,仍然存在一些小问题。element.value被弃用为element.val,或者只是语法错误?
$(document).ready(function() {
<?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>