如果没有值,则禁用下拉列表并移至下一个下拉列表

时间:2015-12-28 16:10:47

标签: php ajax codeigniter

我使用ajax有4个下拉列表。如果在每个相应的下拉列表中找到值,我的工作正常。

但如果第3个下拉列表中没有值,则它不会自动加载到第4个,也不会禁用第3个下拉列表。 Insted,我得到一个空白选项,从第3个下拉列表中选择,如果我选择那么它会加载第4个下拉列表。

Ajax代码:

function selectState(country_id){
	if(country_id!="0"){
		loadData('state',country_id);
		$("#region_dropdown").html("<option value='0'>Select Region</option>");
	}else{
		$("#state_dropdown").html("<option value='0'>Select state</option>");
		$("#region_dropdown").html("<option value='0'>Select Region</option>");
	}
}
function selectRegion(state_id){
	if(state_id!="0"){
		loadData('region',state_id);
		$("#city_dropdown").html("<option value='0'>Select city</option>");
	}else{
		$("#region_dropdown").html("<option value='0'>Select Region</option>");
		$("#city_dropdown").html("<option value='0'>Select city</option>");
	}
}
function selectCity(state_region_id){
	if(state_region_id!="0"){
		loadData('city',state_region_id);
	}else{
		$("#city_dropdown").html("<option value='0'>Select city</option>");
	}
}

	var dataString = 'loadType='+ loadType +'&loadId='+ loadId;
	$("#"+loadType+"_loader").show();
    $("#"+loadType+"_loader").fadeIn(400).html('Please wait... <img src="http://localhost/admin/includes/images/loading.gif" />');
	$.ajax({
		type: "POST",
		url: "loadData",
		data: dataString,
		cache: false,
		success: function(result){
			$("#"+loadType+"_loader").hide();
			$("#"+loadType+"_dropdown").html("<option value='0'>Select "+loadType+"</option>");  
			$("#"+loadType+"_dropdown").append(result);  
		}
	});
}

观看:

<?php if($list->num_rows() > 0){ ?>
<select class="full-width" name="v_item_country" onchange="selectState(this.options[this.selectedIndex].value)">
<option value="0">Country</option>
    <?php foreach($list->result() as $listElement): ?>
        <option value="<?= $listElement->v_country_id?>"><?= $listElement->v_country_name?></option>
    <?php endforeach; ?>
</select>
<select class="full-width" name="v_item_state" id="state_dropdown" onchange="selectRegion(this.options[this.selectedIndex].value)">
    <option value="0">State</option>
</select>
<select class="full-width" name="v_item_region" id="region_dropdown" onchange="selectCity(this.options[this.selectedIndex].value)">
    <option value="0">Region</option>
</select>
<select class="full-width" name="v_item_city" id="city_dropdown">
    <option value="0">City</option>
</select>
<?php }else{ echo 'No Country Name Found'; } ?>

和控制器:

public function loadData()
{
    $loadType=$_POST['loadType'];
    $loadId=$_POST['loadId'];

    $this->load->model('admin/model_users');
    $result=$this->model_users->getData($loadType,$loadId);
    $HTML="";

    if($result->num_rows() > 0){
        foreach($result->result() as $list){
            $HTML.="<option value='".$list->id."'>".$list->name."</option>";
        }
    }
    echo $HTML;
}

型号:

function getCountry(){
    $this->db->select('v_country_id,v_country_name');
    $this->db->from('vbc_country');
    $this->db->order_by('v_country_name', 'asc'); 
    $query=$this->db->get();
    return $query; 
}
function getData($loadType,$loadId){
    if($loadType=="state"){
        $fieldList='id,v_state_name as name';
        $table='vbc_state';
        $fieldName='country_id';
        $orderByField='v_state_name';                       
    }elseif($loadType == "region"){
        $fieldList='id,v_state_region_name as name';
        $table='vbc_state_region';
        $fieldName='state_id';
        $orderByField='v_state_region_name';
    }else{
        $fieldList='id,v_city_name as name';
        $table='vbc_city';
        $fieldName='state_region_id';
        $orderByField='v_city_name';
    }
    $this->db->select($fieldList);
    $this->db->from($table);
    $this->db->where($fieldName, $loadId);
    $this->db->order_by($orderByField, 'asc');
    $query=$this->db->get();
    return $query; 
}

1 个答案:

答案 0 :(得分:1)

试试这个:

public function loadData()
{
    $loadType=$_POST['loadType'];
    $loadId=$_POST['loadId'];

    $this->load->model('admin/model_users');
    $result=$this->model_users->getData($loadType,$loadId);
    $HTML="";

    if($result->num_rows() > 0){
        foreach($result->result() as $list){
            $HTML.="<option value='".$list->id."'>".$list->name."</option>";
        }
    }
    else {
      $HTML.="<option value='disabled' disabled>No results found...</option>";
    }
    echo $HTML;
}