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

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

使用輸入類型圖像上傳前預覽圖像

使用輸入類型圖像上傳前預覽圖像

嚕嚕噠 2022-12-29 15:32:13
如果我使用img標簽并在單擊簡單按鈕后 - 圖像預覽工作:HTML<form runat="server">  <input type='file' id="imgInp" />  <img id="blah" src="#" alt="your image" /> <--- in this place img</form>JSfunction readURL(input) {  if (input.files && input.files[0]) {    var reader = new FileReader();        reader.onload = function(e) {      $('#blah').attr('src', e.target.result);    }        reader.readAsDataURL(input.files[0]); // convert to base64 string  }}$("#imgInp").change(function() {  readURL(this);});但是當我試圖隱藏按鈕并使圖像可點擊(使用實現它input type="image")預覽不起作用時,選擇圖像沒有效果,不更改默認圖像)。默認的可點擊圖片類似于“點擊此處上傳照片”。HTML<form runat="server">                   <div class="thumb-preview">        <input type="image" id="blah" src="<?=path?>/images/upload.png"/> <-- input type image        <input type='file' id="imgInp" style="display: none;" />    </div>                            </form>JSfunction readURL(input) {  if (input.files && input.files[0]) {    var reader = new FileReader();        reader.onload = function(e) {      $('#blah').attr('src', e.target.result);    }        reader.readAsDataURL(input.files[0]); // convert to base64 string  }}$("#imgInp").change(function() {  readURL(this);});$("input[type='image']").click(function() {    $("input[id='imgInp']").click();});你有想法如何改變形象input type="image"嗎?
查看完整描述

1 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

您需要在點擊功能中使用e.PreventDefault方法,以確保您每次點擊時click type=image都不是該頁面。!reloading

現場演示:


function readURL(input) {

  if (input.files && input.files[0]) {

    var reader = new FileReader();

    reader.onload = function(e) {

      $('#blah').attr('src', e.target.result);

    }

    reader.readAsDataURL(input.files[0]); // convert to base64 string

  }

}


$("#imgInp").change(function(e) {

  e.preventDefault()

  readURL(this);

});


$("input[type='image']").click(function(e) {

  e.preventDefault()

  $("input[id='imgInp']").click();

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form runat="server">               

    <div class="thumb-preview">

        <input type="image" id="blah" src="https://img.icons8.com/dotty/80/000000/upload.png"/> 

        <input type='file' id="imgInp" style="display: none;" />

    </div>                            

</form>




查看完整回答
反對 回復 2022-12-29
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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