从自动完成下拉列表中选择用户后,用户名出现在输入字段中,现在我想显示用户图像。选择后默认图像确实发生变化,但它不会加载用户图像?我觉得我错过了一些东西var img = ui.item.label;
$("#pic").attr("src",img);我在查找时看到一个与此类似的问题(show-image-in-jquery-ui-autocomplete),但是这个问题不使用json。请帮忙?索引.PHP<body> <br /> <br /> <div class="container"> <br /> <div class="row"> <div class="col-md-3"> <img id="pic" src="images/default-image.png"> </div> <div class="col-md-6"> <input type="text" id="search_data" placeholder="Enter Student name..." autocomplete="off" class="form-control input-lg" /> </div> <div class="col-md-3"> </div> </div> </div> </body></html><script> $(document).ready(function(){ $('#search_data').autocomplete({ //gets data from fetch source: "fetch.php", minLength: 1, select: function(event, ui) { $('#search_data').val(ui.item.value); //$("#pic").val(ui.item.img); var img = ui.item.label; $("#pic").attr("src",img); }, }) .data('ui-autocomplete')._renderItem = function(ul, item){ //renders the item in a ul return $("<li class='ui-autocomplete-row'></li>") //formats the row in css .data("item.autocomplete", item) //auto complete item .append(item.label) .appendTo(ul); }; });</script>FETCH.phpif(isset($_GET["term"])){ $connect = new PDO("mysql:host=localhost; dbname=tests_ajax", "root", ""); $query = " SELECT * FROM tbl_student WHERE student_name LIKE '%".$_GET["term"]."%' ORDER BY student_name ASC "; $statement = $connect->prepare($query); $statement->execute(); $result = $statement->fetchAll(); $total_row = $statement->rowCount();
1 回答
UYOU
TA贡献1878条经验 获得超4个赞
您将返回整个<img>
元素(和附加文本):
$temp_array['label'] = '<img src="images/'.$row['image'].'" width="70" /> '.$row['student_name'].'';
所以当你这样做时:
var img = ui.item.label; $("#pic").attr("src",img);
你试图最终得到的是这样的:
<img id="pic" src="<img src="images/someFile.jpg" width="70" /> Some Name">
这当然只是一堆语法错误。
如果您只想设置src
现有值<img>
,则仅返回该src
值:
$temp_array['img'] = 'images/'.$row['image'];
并将 设为src
该值:
$("#pic").prop("src", ui.item.img);
- 1 回答
- 0 关注
- 94 浏览
添加回答
举报
0/150
提交
取消