我一直在用自动完成选择功能显示图像。当我想设置img src时,悬停地址显示不同的路径作为当前页面的URL。这是代码
查看代码
<span class="pimg">
<a href="#">
<picture>
<input class="spec_blk" readonly name="image" value=""> </div>
<img id="resimler">
</picture>
</a>
</span>
我的jquery代码
$(document).ready(function(){
$('#title').autocomplete({
source: "<?php echo site_url('home/get_autocomplete');?>",
select: function (event, ui) {
$('[name="title"]').val(ui.item.label);
$('[name="image"]').val(ui.item.image);
$( "#resimler" ).attr( "src", "localhost/uploads/+ ui.item.image );
}
});
});
最后一个控制器
function get_autocomplete(){
$this->load->model('apsisx');
if (isset($_GET['term'])) {
$result = $this->apsisx->search_comp($_GET['term']);
if (count($result) > 0) {
foreach ($result as $row)
$arr_result[] = array(
'label' => $row->p_name,
'image' => $row->image,
);
echo json_encode($arr_result);
}
}
}
问题可以在https://prnt.sc/u8wa0n
图像路径localhost/uploads/20200826160809_723647.jpg
结果路径localhost/current_page_url/localhost/uploads/20200826160809_723647.jpg
在"localhost/uploads/
更改
$( "#resimler" ).attr( "src", "localhost/uploads/+ ui.item.image );
到此
$( "#resimler" ).attr( "src", "localhost/uploads/"+ ui.item.image );