2014-12-02 34 views
0

這是一個笨+ jQuery的問題......它可能是小白,但請原諒我...... 我有這樣的模式:jQuery的IMG src屬性不能出現一些值

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中未定義。但是,所有提醒正確的'名稱',但圖片不會出現...請幫助嗎?

+0

id在html中是唯一的。#照片ID是重複的,我認爲 – 2014-12-02 09:23:56

+0

顯示您的正確的html代碼 – 2014-12-02 09:24:43

+0

hi @JqueryKing我編輯了代碼來添加html代碼。我究竟做錯了什麼? – 2014-12-02 14:47:50

回答

0

似乎有另一行冗餘代碼做類似的改變#photo src的東西,這是使得它有時有時不工作的原因。