我有一个从 MySQL 表为 TinyMCE动态生成的链接列表。这可以正常显示表中的所有链接。现在我试图通过 ajax 将一个值传递给生成链接列表的 php 文件,以根据子学科缩小查询范围:
<script type="text/javascript">
$(document).ready(function() {
$("#sub_discipline").change(function(){
$.ajax({
url: "../scripts/tiny_mce/lists/link_list.js.php",
type: "post",
data: {
sub_discipline : $('#sub_discipline').val()
},
success: function(data){
}
});
});
}
);
</script>
问题是链接列表现在没有显示在 TinyMCE 中,可能是因为该值没有被传递。我可以通过传递 URL 字符串中的值的 link_list.js.php 获得结果,因此发生了其他事情。要么是上面的 ajax 调用错误,要么是 TinyMCE 在编辑器加载时加载链接列表,之后无法更新列表。
仅供参考,如果这可以帮助其他人,link_list.js.php 可以很好地为 TinyMCE 创建链接列表:
<?php Header("content-type: application/x-javascript"); ?>
<?php
require_once('../../../connection/connect.php');
mysql_select_db($database, $connection);
$sub_discipline = $_POST['sub_discipline'];
$query = "SELECT link_title, url FROM link WHERE sub_discipline_fk = '$sub_discipline'";
$result = mysql_query($query, $connection) or die(mysql_error());
$links = array();
while($row = mysql_fetch_assoc( $result)){
$links[$row['link_title']] = $row['url'];
}
$o = null;
$count = count($links);
$i = 1;
foreach($links as $text => $url){
$o .= sprintf(('["%1$s", "%2$s"]'),
$text,
$url
);
if($i < $count)
$o .= ',';
$i++;
}
?>
var tinyMCELinkList = new Array(
<?php echo $o; ?>
);
链接列表作为选项添加到 tinyMCE.init 中:
<script type="text/javascript">
tinyMCE.init({
// General options
mode : "textareas",
theme : "advanced",
plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
// Theme options
theme_advanced_buttons1 : "print,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,formatselect,fontsizeselect,|,forecolor,backcolor",
theme_advanced_buttons2 : "cut,copy,paste,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,insertdate,inserttime,preview,|,fullscreen",
theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,|,sub,sup,|,charmap,emotions,iespell,media,|,ltr,rtl",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
theme_advanced_resizing : true,
// Skin options
skin : "o2k7",
skin_variant : "silver",
// Drop lists for link/image/media/template dialogs
external_link_list_url : "../scripts/tiny_mce/lists/link_list.js.php",
external_image_list_url : "js/image_list.js",
media_external_list_url : "js/media_list.js",
});
</script>