亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

選擇后顯示圖像: function(event, ui)

選擇后顯示圖像: function(event, ui)

PHP
狐的傳說 2023-07-01 17:26:52
從自動完成下拉列表中選擇用戶后,用戶名出現在輸入字段中,現在我想顯示用戶圖像。選擇后默認圖像確實發生變化,但它不會加載用戶圖像?我覺得我錯過了一些東西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" />&nbsp;&nbsp;&nbsp;'.$row['student_name'].'';

所以當你這樣做時:

var img = ui.item.label;
$("#pic").attr("src",img);

你試圖最終得到的是這樣的:

<img id="pic" src="<img src="images/someFile.jpg" width="70" />&nbsp;&nbsp;&nbsp;Some Name">

這當然只是一堆語法錯誤。

如果您只想設置src現有值<img>,則僅返回該src值:

$temp_array['img'] = 'images/'.$row['image'];

并將 設為src該值:

$("#pic").prop("src", ui.item.img);


查看完整回答
反對 回復 2023-07-01
  • 1 回答
  • 0 關注
  • 122 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號