0
我是新來的JQuery,我在我的代碼中使用PHP。我不知道如何進行動態下拉。當用戶從第一個下拉菜單選擇區域時,接下來他會從另一個下拉菜單中選擇 - 這個區域位於該區域。請幫忙! 我的代碼是:如何使用jquery動態下拉
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
<?php
echo form_open();
echo "<table border = '0' >";
echo "<tr><td> Region:* </td><td>";
echo "<select name = 'region[]' id='region' >";
foreach($regions as $row)
{
echo '<option value= "'.$row->region.'">'.$row->region.'</option>';
}
echo "</select>";
echo "</td></tr>";
echo "<tr><td> School:* </td><td>";
echo "<select name = 'school[]' id='school'>";
foreach($school_show as $row)
{
echo '<option value="'.$row->school_name.'">'.$row->school_name.'</option>";
}
echo "</select>";
echo "</td></tr>";
echo form_submit($data);
echo "</form>";
echo "</table>";
控制器是:
<?php
class Home extends CI_Controller {
public function register()
{
$this->load->model('user_model');
$data['dynamic_view'] = 'register_form';
$data['regions'] = $this->user_model->regions_show();
$data['school_show'] = $this->user_model->school_show();
$this->load->view('templates/main',$data);
}
}
型號是:
<?php
class User_model extends CI_Model {
public function __construct() {
parent:: __construct();
$this->load->database();
$this->load->helper('array');
}
public function regions_show() {
$this->db->select('region');
$this->db->distinct('region');
$this->db->from('schools');
$result=$this->db->get();
return $result->result();
}
public function school_show() {
$this->db->select('school_name');
$this->db->from('schools');
$result=$this->db->get();
return $result->result();
}
}
當用戶在第一個下拉列表中選擇一個區域,你想閱讀使用'$該區域的名稱(這一點).VAL()'。然後用ajax調用這個值來發送一個PHP文件,該文件將讀取數據庫中該區域的所有學校,並將列表發送回jQuery(通常以JSON格式)。然後,迭代這個json並構造/更新第二個下拉列表。有成千上萬的關於這個的教程。 – 2015-04-02 09:57:32
是的,這就是我想要的。我嘗試了一些教程,但由於我不知道jQuery,我沒有這樣做。它沒有工作。 – 2015-04-02 10:16:13