【发布时间】:2012-01-03 13:25:54
【问题描述】:
我有以下看法:
<form action=<?=$target;?> method="post" name="editForm">
<div id="page1">
<select id="categories" name="category" onmouseover="populateCategory(<?=$result?>, <?=$id?>)" >
</select>
</div>
</form>
在选择标签上使用以下 javascript 函数:
function populateCategory(subCategories, id) {
var select = document.getElementById(id);
var opt = document.createElement("option");
opt.text = "--Please make a selection--";
for (i = 0; i < subCategories.length; i++) {
var opt = document.createElement("option");
opt.value = i;
opt.text = subCategories[i];
select.add(opt);
}
}
传递给函数的参数是以下变量:
$result = $this->getCategories(0);
$id = 'categories';
调用以下函数
public function getCategories($parent, $published = 1) {
//Sanitize Params...
//$sanParent = $this->db->getEscaped($parent);
//$sanPublished = $this->db->getEscaped($published);
$query =
"SELECT * FROM #__adsmanager_categories
WHERE parent = " . $parent .
" AND published = " . $published;
$this->db->setQuery($query);
$result = $this->db->loadAssocList();
var_dump($parent);
if (!$result) {
return false;
}
return $result;
}
低调:
我发布的最后一个函数向数据库发出请求,获取旨在用于创建下拉列表的类别列表。我认为如果我将获得的类别传递给 JavaScript 函数,我可能能够以简单的方式填充它。我做错了吗?我觉得这是一个 JavaScript 问题,因为函数调用似乎没有发生。例如,当我打开浏览器调试器时,没有迹象表明它是否被调用。
更新:
这是 html-source 列出的内容:
<script type="text/javascript">
function populateCategory(subCategories, id) {
var select = document.getElementById(id);
var opt = document.createElement("option");
opt.text = "--Please make a selection--";
for (i = 0; i < subCategories.length; i++) {
var opt = document.createElement("option");
opt.value = i;
opt.text = subCategories[i];
select.add(opt);
}
}
</script>
<form action=/index.php?option=com_adsmanager&task=write&Itemid=1 method="post" name="editForm">
<div id="page1">
<select id="categories" name="category" onmouseover="populateCategory(Array, categories)" >
</select>
</div>
</form>
【问题讨论】:
-
你在哪里声明这个变量?
subCategories在你的 JS 函数中。 -
您的问题与 AJAX 有什么关系?还是标签加错了?
-
I have a feeling it's a JavaScript issue- 如果你有这种感觉,你必须在这里放一段JS代码,而不是PHP代码。 -
我确实发布了 JS 代码。如果您查看我列出的第一个函数并读取它,您会注意到它是 JavaScript。 ;) 就 AJAX 参考而言,这是我的一个错误。为清晰起见进行编辑。
-
@Lance 已修复。我尝试在我的代码中纠正它,但不幸的是它仍然没有做任何事情。
标签: php javascript mysql html