gpt4 book ai didi

javascript - 传递给javascript中的函数时出现Uncaught ReferenceError

转载 作者:行者123 更新时间:2023-11-30 14:41:55 30 4
gpt4 key购买 nike

我正在尝试使用基于按钮点击的表格中的数据填充表格。我正在将数据传递到 javascript 函数中。

<?php 
$sql=mysqli_query($con, "SELECT FacultyEducationalId, FacultyDegreeName, FacultyCollegeName, FacultyPassingYear FROM facultyeducationaltable WHERE FacultyId=".$_SESSION['userid']);
while ($row=mysqli_fetch_array($sql))
{
?>
<tr class="info">
<td><?php echo $row['FacultyDegreeName'] ?></td>
<td><?php echo $row['FacultyCollegeName'] ?> </td>
<td><?php echo $row['FacultyPassingYear'] ?> </td>
<td>
<button onclick="modifyEdu(<?php echo $row['FacultyEducationalId'].','.$row['FacultyDegreeName'].','.$row['FacultyCollegeName'].','.$row['FacultyPassingYear']; ?>)" id="modifyEdu" class="btn btn-primary">Modify</button>
</td>
</tr>
<?php
}
?>

函数如下:

<script type="text/javascript">
function modifyEdu(id, deg, uniname, passyear) {
var facultyeduid=parseInt(id);
var degname=deg;
var uniname=uniname;
var passyear=passyear;
console.log("1");
document.getElementById("degreename").value="2";
document.getElementById("universityname").value=uniname;
document.getElementById("passyear").value=passyear;
}
</script>

我收到:Uncaught ReferenceError: x is not defined at HTMLButtonElement.onclick。我该如何解决? (x 是“FacultyDegreeName”中的值)

最佳答案

您缺少使用单引号 '\' 对字符串进行转义:

<button onclick="modifyEdu('<?php echo $row['FacultyEducationalId'].'\',\''.$row['FacultyDegreeName'].'\',\''.$row['FacultyCollegeName'].'\',\''.$row['FacultyPassingYear']; ?>')" id="modifyEdu" class="btn btn-primary">Modify</button>

因为您将得到 modifyEdu(foo,bar,2018) 而不是 modifyEdu('foo','bar','2018')

在第一种情况下,foo 将被视为 undefined variable 并导致“ReferenceError” 错误。

关于javascript - 传递给javascript中的函数时出现Uncaught ReferenceError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49518963/

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