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

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

將文件上傳到 html 輸入時如何自動加載?

將文件上傳到 html 輸入時如何自動加載?

繁星點點滴滴 2023-09-18 15:58:41
當我將文件放入 html 輸入中時,我想通過 ajax post 上傳。(無需點擊)它在我單擊當前代碼中的啟動按鈕后起作用。我的代碼    $(document).ready(function () {  $("#but_upload").click(function () {    $('#img-loag-scrin').html('');    $('div#img-loag-scrin').css('display', 'block').prepend('<img id="img_akb" src="https://' + HTTP + '/images/ajax-loader.gif" id="buff-load" style="display:block;margin:10px auto; cursor: wait;">');    var fd = new FormData();    var files = $('#file')[0].files[0];    fd.append('file', files);    $.ajax({      url: 'https://localhost.uz/ajax/ajax_img.php?act=upload',      dataType: "json",      type: 'post',      cache: false,      data: fd,      contentType: false,      processData: false,      success: function (response) {        if (response != 0) {          $('#img-loag-scrin').prepend(response.scrins);          $('#textrea').val($('#textrea').val() + " " + response.textrea);          $('#img_akb').remove();        } else {          alert('File not uploaded');        }      }    });  });});HTML代碼<form method="post" action="" enctype="multipart/form-data" id="myform">  <input type="file" id="file" name="file">  <input type="button" class="sf_button" value="Upload" id="but_upload"></form>當 html 插入到輸入文件中時,無需單擊按鈕,就應該加載該文件。加載圖像后,圖像必須從輸入中消失
查看完整描述

1 回答

?
慕俠2389804

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

我用過改變。


$(document).ready(function () {

  $('#file').unbind().on('change', function () {

    $('#img-loag-scrin').html('');

    $('div#img-loag-scrin').css('display', 'block').prepend('<img id="img_akb" src="https://' + HTTP + '/images/ajax-loader.gif" id="buff-load" style="display:block;margin:10px auto; cursor: wait;">');

    var fd = new FormData();

    var files = $('#file')[0].files[0];

    fd.append('file', files);

    $.ajax({

      url: 'https://localhost/ajax/ajax_img.php?act=upload',

      dataType: "json",

      type: 'post',

      cache: false,

      data: fd,

      contentType: false,

      processData: false,

      success: function (response) {

        if (response != 0) {

          $('#img-loag-scrin').prepend(response.scrins);

          $('#textrea').val($('#textrea').val() + " " + response.textrea);

          $('#img_akb').remove();

        } else {

          alert('File not uploaded');

        }

      }

    });

  });

});

html代碼


<form method="post" action="" enctype="multipart/form-data" id="myform">

   <input type="file" id="file" name="file">

</form>


查看完整回答
反對 回復 2023-09-18
  • 1 回答
  • 0 關注
  • 94 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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