1

我有一个类似的问题:Code Igniter - form_dropdown 从数据库中选择正确的值,但在这种情况下,我有 2 个下拉列表,州和城市(使用 JavaScript)。根据用户在州下拉菜单中选择的内容重新填充城市的下拉选项。

例如,当用户选择一个州(例如:纽约)时,“城市”的下拉选项将仅变为纽约的城市(例如:奥尔巴尼、阿姆斯特丹等)。

用户选择一个值后,点击保存,将其保存到数据库中。

问题是,我如何让下拉菜单自动选择用户在初始阶段选择的那个?如果它只有 1 个下拉选项,我可以做到。但在这种情况下,City 的下拉菜单会根据用户在 State 下拉选项中选择的内容重新填充。

控制器:

//this one i managed to get it automatically choose the one that's been selected by the user in the initial stage    
$username=$this->session->userdata('username'); 
$data['orgtype'] = $this->m_user->get_orgtype_dropdown($username);

//these two are the problem 
$data['state'] = $this->m_user->get_state_dropdown($username); 
$data['city'] = $this->m_user->get_city_dropdown($username);

模型:

function get_orgtype_dropdown($username){
$sqlstr="SELECT * FROM a01 WHERE username='$username'";
$hslquery=$this->db->query($sqlstr);
   foreach($hslquery->result_array() as $row){
        $return[$row['orgtype']] = $row['orgtype'];
        }
    return $return;
}

function get_state_dropdown($username){
$sqlstr="SELECT * FROM a01 WHERE username='$username'";
$hslquery=$this->db->query($sqlstr);       
   foreach($hslquery->result_array() as $row){
        $return[$row['state']] = $row['state'];
        }
    return $return;
}

function get_city_dropdown($username){
$sqlstr="SELECT * FROM a01 WHERE username='$username'";
$hslquery=$this->db->query($sqlstr);       
   foreach($hslquery->result_array() as $row){
        $return[$row['city']] = $row['city'];
        }
    return $return;
}

看法:

<?php
$orgtypeOption = array(
         'Academic'  => 'Academic',
         'Professional'    => 'Professional',                  
         );

    echo form_label("Organization Type : ");
    echo form_dropdown('orgtype', $orgtypeOption, $orgtype);
    echo br();
    echo form_label("State : ");            
    ?>
    <select name ="state" id="countrySelect" size="1" onChange="makeSubmenu(this.value)">
        <option></option>
        <option value="USA" <?php if ($state=="USA") echo 'selected="selected"';?>>USA</option>
        <option value="Singapore" <?php if ($state=="Singapore") echo 'selected="selected"';?>>Singapore</option>
        <option value="Jawa Timur" <?php if ($state=="Jawa Timur") echo 'selected="selected"';?>>Jawa Timur</option>
        <option value="Jawa Barat" <?php if ($state=="Jawa Barat") echo 'selected="selected"';?>>Jawa Barat</option>
    </select>
    <?php
    echo br();
    echo form_label("City : ");
    ?>
    <select name="city" id="citySelect" size="1">
        <option></option>
    </select>

JavaScript:

var citiesByState = {
    USA: ["NY","NJ"],
    Singapore: ["taas","naas"],
    "Jawa Timur": ["Surabaya","Malang"],
    "Jawa Barat": ["Bandung","Banjar"]
};
function makeSubmenu(value) {
    if(value.length==0) document.getElementById("citySelect").innerHTML = "<option></option>";
    else {
        var citiesOptions = "";
        for(cityId in citiesByState[value]) {
            citiesOptions+="<option>"+citiesByState[value][cityId]+"</option>";
        }
        document.getElementById("citySelect").innerHTML = citiesOptions;
    }
}
function displaySelected() {
    var country = document.getElementById("countrySelect").value;
    var city = document.getElementById("citySelect").value;
    alert(country+"\n"+city);
}
function resetSelection() {
    document.getElementById("countrySelect").selectedIndex = 0;
    document.getElementById("citySelect").selectedIndex = 0;
}

这是预览:应用程序预览图片

请帮忙

4

0 回答 0