这是一个codeigniter + jquery问题......它可能是菜鸟但请原谅我...... 我有这个模型:
function get_base_photo($model_selected) {
if ($model_selected != NULL) {
$this - > db - > select('md.modelID,md.model_photo') - > from('model md');
$this - > db - > where(array('md.active' = > 1, 'md.modelID' = > $model_selected));
}
$query = $this - > db - > get();
$photos = array();
if ($query - > result()) {
foreach($query - > result() as $photo) {
$photos[$photo - > modelID] = $photo - > model_photo;
}
return $photos;
} else {
return FALSE;
}
$query - > result();
return $query - > result();
}
这是我的控制者:
function base_photo($selected_car){
header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->config_model->get_base_photo($selected_car)));
}
这是我的jquery:
$.ajax({
type: "POST",
url: base_url + "configurator/base_photo/" + modelID,
success: function (response) {
$.each(response, function (id, name) {
alert('name');
$("#photo").attr("src", base_url + "assets/img/wheels/" + name);
});
},
error: function () {
alert('error document ready photos');
}
});
这是我的HTML
<div id="top">
<?php echo form_open('configurator/index'); ?>
<select id="model_selected" name="model_selected">
<?php
foreach($cars as $car){
if(isset($_POST['model_selected']) && $_POST['model_selected'] == $car->modelID){
echo '<option value = '.$car->modelID.' selected>'.$car->modelName.' - '.$car->model_photo.'</option>';
}
else
{
echo '<option value = '.$car->modelID.'>'.$car->makeName.' - '.$car->modelName.' </option>';
}
}
echo form_submit('car_submit','submit');
?>
</select>
</div><!--end top div-->
<div id="content">
<div id="left_content">
<div id="image_holder">
<?php if(isset($_POST['car_submit'])){ ?>
<img src="<?php echo base_url();?>assets/img/wheels/<?php echo $first_car->model_photo;?>" width="300px" />
<?php }elseif(isset($_POST['top_submit'])){ ?>
<img src="<?php echo base_url();?>assets/img/wheels/<?php echo $top_price->photo;?>" width="300px" />
<?php }elseif(isset($_POST['middle_submit'])){ ?>
<img src="<?php echo base_url();?>assets/img/wheels/<?php echo $middle_price->photo;?>" width="300px" />
<?php }elseif(isset($_POST['side_submit'])){ ?>
<img src="<?php echo base_url();?>assets/img/wheels/<?php echo $side_price->photo;?>" width="300px" />
<?php }elseif(isset($_POST['bottom_submit'])){ ?>
<img src="<?php echo base_url();?>assets/img/wheels/<?php echo $bottom_price->photo;?>" width="300px" />
<?php }elseif(isset($_POST['ring_submit'])){ ?>
<img src="<?php echo base_url();?>assets/img/wheels/<?php echo $ring_price->photo;?>" width="300px" />
<?php }else{ ?>
<img id="photo" src="<?php echo base_url();?>assets/img/wheels/wheel.png" width="300px" />
<?php } ?>
</div><!--end image holder-->
</div><!--end left content-->
<div id="right_content">
<table>
<th>Section</th><th>Material</th><th>Price</th><th>Color</th>
<?php echo form_open('configurator/top'); ?>
<tr><!-- top row-->
<td>Top</td>
<td>
<select name="top_material" id="top_material">
</select>
</td>
<td><input type="text" id="top_price" name="top_price" value="" readonly /></td>
<td>
<select name="top_color" id="top_color">
</select>
</td>
</tr><!-- end top row-->
虽然它适用于某些modelID的某些图片,但有些在img src中返回undefined。但是,所有人都提醒正确的姓名&#39;但图片不会出现......请帮帮忙?
答案 0 :(得分:0)
似乎有另一行冗余代码正在做类似改变#photo src的事情,这使得它有时会工作,有时却没有。