HTML/PHP/JS问题- onclick功能不工作

HTML/PHP/JS issue - onclick function not working

本文关键字:功能 工作 onclick 问题 PHP JS HTML      更新时间:2023-09-26

所以在这里我试图调用一个函数,传递2个值作为参数(使用php动态添加)。不管我怎么努力,这该死的东西似乎都不管用。你们有谁能指出我做错了什么吗?下面是代码:

// HTML/PHP
$pid = $row['postID'];
$pt = $row['postTitle'];
//There arrays are working great.
<a href="#" onclick="<?php echo "delpost($pid, '$pt')"; ?>">Delete</a> // Does not call delpost
<a href="#" onclick="delpost(<?php echo $pid; ?>)">Delete</a> // BUT if i remove the second parameter, it works! How? 
// JS
<script language="JavaScript" type="text/javascript">
function delpost(id, title) {
    if (confirm("Are you sure you want to delete" + title + "?")) {
        window.location.href = 'index.php?delpost=' + id;
      }
}
</script>

"delpost上的引号错误。delpost是一个函数,所以onclick"<?php echo delpost($pid,$pt); ?>"应该工作。

Delete//不调用delpost Delete//但是如果我删除第二个参数,它工作!如何?

first delete不工作,因为当你向函数传递两个值时一个值是空的,为什么它不工作

<?
$pid = 1;
$pt = 2;
//There arrays are working great.
?>
<a href="#" onclick="delpost(<?php echo $pid?>,<?php echo $pt?>)">Delete</a> // Does not call delpost
<a href="#" onclick="delpost(<?php echo $pid; ?>)">Delete</a> // BUT if i remove the second parameter, it works! How? 

<script language="JavaScript" type="text/javascript">
function delpost(id, title) {
    alert(id);
    if (confirm("Are you sure you want to delete" + title + "?")) {
        window.location.href = 'index.php?delpost=' + id;
      }
}
</script>
<a href="#" onclick="delpost(<?=$pid?>, '<?=$pt?>')">Delete</a>

我直接尝试了你的代码,它工作了,但我也添加了另一种写它的方式:

<?php
  $pid = 42;
  $pt = 'Awesometitle 2000';
?>
<a href="#" onclick="<?php echo "delpost($pid, '$pt')"; ?>">Delete</a>
<a href="#" onclick="<?php echo 'delpost(' . $pid . ', ''' . $pt . ''');'; ?>">Delete 2</a>

<script>
function delpost(id, title) {
  console.log(id);
  console.log(title);
}
</script>

两个变量都应该在控制台中打印42和"Awesometitle 2000"