我使用的是智能和php。我有两个下拉菜单。另外,我有两个数据库,一个有类别,另一个有新闻,它有分类id。其中一个菜单读分类。我希望当我选择从第一个菜单类别,页面自动刷新,并放入第二个下拉新闻与该类别。
<form method="post">
<h3>Category of news</h3>
<select name="categoriesForm" id="news_cat">
<option value="0"></option>
{foreach from=$categories item=i}
<option value="{$i.id}"> {$i.name|stripslashes} </option>
{/foreach}
</select>
<h3 style="position:absolute;left:500px; top:130px;">Name of news</h3>
<select name="news" id="news_name" style="position:absolute;left:500px; top:190px;">
<option value="0"></option>
{foreach from=$news item=i}
<option value="{$i.id}"> {$i.name|stripslashes} </option>
{/foreach}
</select>
</form>这是控制者:
function edit_news(){
$cat = $this->news->getCategoriesNews();
$this->assign('categories',$cat);
$selected_key = $_POST['categoriesForm'];
$news = $this->news->getNameNews($selected_key);
$this->assign('news',$news);
} 这就是模型
function getCategoriesNews(){
return $this->db->GetAll("SELECT id, name FROM categories ");
}
function getNameNews($category){
return $this->db->GetAll("SELECT name,cat_id FROM news WHERE cat_id = '$category' ");
}发布于 2013-11-11 09:21:21
我已经提到了这个过程的概述。我相信,只要少做功课,你就能实现你的功能。
1)使用onChange on categoriesForm下拉列表调用Javascript / jQuery函数。因此,每当您更改categoriesForm的值时,都会调用脚本函数。
<select name="categoriesForm" id="news_cat" onChange="feed_secondary()">2) feed_secondary获取categoriesForm的当前值并触发和ajax调用。
3)在ajax调用中,传递当前类别的值。
( 4)在控制器中,对category_id进行处理,并发送该类别的所有新闻作为响应。
4)在feed_secondary中,处理响应并操作二级下拉news。
https://stackoverflow.com/questions/19902560
复制相似问题