我需要将这些值传递给头部的 javascript 函数。问题是,当从数据库中提取时,一些单词不止一个,使用 javascript:function(vars) 传递时,使用 href 它会在一个空格处将其截断,我根本无法让 onclick 工作。我在变量(“”、“”、/”和{)周围尝试了各种引号和括号组合,以及在我知道在传递前会有多个单词的变量周围加上引号(见下文) .这里是PHP:
<?php
require('config/dbconfig.php');
$query = "SELECT * FROM news ORDER BY id DESC LIMIT 4";
if ($stmt = $mysqli->prepare($query)) {
/* execute statement */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($idn, $titlen, $categoryn, $descn, $postdaten, $authorn);
/* fetch values */
while ($stmt->fetch()) {
//echo 'id: '. $id .' title: '. $title;
echo "<table border='0'>";
$shortDescLengthn = strlen($descn);
if ($shortDescLengthn > 106) {
$sDCutn = 106 - $shortDescLengthn;
$shortDescn = substr($descn, 0, $sDCutn);
} else {
$shortDescn = $descn;
}
$titlenQuote = "'". $titlen ."'";
$descnQuote = "'". $descn ."'";
$authornQuote = "'". $authorn ."'";
echo "
<h1>". $titlen ."</h1>
<tr><td>". $shortDescn ."...</td></tr>
<tr><td><a href='javascript:return false;' onclick='readMore(". $idn .",". $titlenQuote .",". $categoryn .",
". $descnQuote .",". $postdaten .",". $authornQuote .")'>Read More</a></td></tr>
<tr><td>Written by: " . $authorn . "</td></tr>
<tr><td><img src='images/hardcore-games-newsbar-border.png' width='470px' /></td></tr>
";
}
echo "</table><br />";
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
?>
它的功能是:
<script type="text/javascript">
function readMore(id,title,cat,desc,post,auth) {
alert(id +","+ title +","+ cat +","+ desc +","+ post +","+ auth);
var $dialog = $('<div></div>').html('This dialog will show every time!').dialog({autoOpen: false,title: 'Basic Dialog'});
$dialog.dialog('open');
$dialog.title = title;
$dialog.html(desc);
}
</script>
这是在主索引页面中使用 load() 的页面。我也遇到了对话问题,但它甚至还没有达到这一点,所以这是下一步。如果需要知道,所有 jquery 包含都在主索引页面上。load() 将进入索引页面上的一个 div,到目前为止效果很好。