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

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

Ajax 沒有從動態創建的表單中獲取 php 帖子

Ajax 沒有從動態創建的表單中獲取 php 帖子

PHP
瀟瀟雨雨 2021-09-05 17:59:18
我創建了一些 ajax 上傳圖像并將其加載到頁面中。它會在頂角創建一個帶有 X 按鈕的圖像,我正在嘗試獲取它,因此當單擊此按鈕時,我會運行另一段 php 代碼,將刪除正確的圖像并重新加載圖像。我無法讓我的 ajax 代碼來獲取 php 代碼,我不知道為什么。任何指出都會非常有幫助。我發現 dymanically 創建的 elemets 不會被拾取所以不得不將我的 ajax 代碼更改為$("body").on("click", "#deleteform button", function(e){所以我達到了這一點,但它仍然沒有獲取我的 php 代碼,我不知道為什么。任何指針都會非常有幫助AJAX JS:$(document).ready(function(){    $("#uploadForm").on('submit',function(e) {        e.preventDefault();        $.ajax({              url: "include_advert/advert_new_gun_add_image.inc.php",          type: "POST",          data:  new FormData(this),          contentType: false,          cache: false,          processData:false,          success: function(data)            {          $("#targetLayer").html(data);            },            error: function()             {            }                     });      });    });      $(document).ready(function(){        $('button.deleteimage').on('click', function(){            var image_id = parseInt($(this).parent().attr('id').replace('deleteform', ''));            console.log(image_id); // You can comment out this. Used for debugging.            e.preventDefault();            $.ajax({                url: "include_advert/advert_new_gun_delete_image.php",                type: "POST",                data:  {image_id: image_id},                contentType: false,                cache: false,                processData:false,                success: function(data)                {                    $("#targetLayer"+image_id).html(data); // targetLayer is dynamic and is different for each record                },                error: function(xhr, status, error) {                    alert(xhr.responseText);                }            });        });    });
查看完整描述

3 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

   $(document).ready(function(){

        $('button.delete-button').on('click', function(){

            var image_id = parseInt($(this).parent().attr('id').replace('deleteform', ''));


            console.log(image_id); // You can comment out this. Used for debugging.


            e.preventDefault();

            $.ajax({

                url: "include_advert/advert_new_gun_delete_image.php",

                type: "POST",

                data:  {image_id: image_id},

                contentType: false,

                cache: false,

                processData:false,

                success: function(data)

                {

                    $("#targetLayer"+image_id).html(data); // targetLayer is dynamic and is different for each record

                },

                error: function(xhr, status, error) {

                    alert(xhr.responseText);

                }

            });

        });

    });

include 'advert_new_gun_save_image_script.inc.php';

include 'advert_new_dropdown_populate/advert_new_gun_image_populate.php';


$imagecount = 0;

echo ('<div class=row sm>');


foreach ($getadvertimages as $getadvertimages_row) {

    echo ( '<div class="image-area" >

    <form  id="deleteform'.$getadvertimages_row['image_id'].'" method = "POST" action ="include_advert/advert_new_gun_delete_image.php" >

    <img src="'. $getadvertimages_row['image_src'] . '"  alt="Preview">

    <button type="button" name="deleteimage" value="" class="remove-image delete-button" style="display: inline;" >X</button>


    </form>

    </div>');

}

echo ('</div>');


同樣,您可以使用 image_id 值使“targetLayer”動態化,就像我使用表單的屬性 id deleteform 所做的那樣。


查看完整回答
反對 回復 2021-09-05
?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

上面的答案幾乎就在那里,但我不得不將其更改 $('button.delete-button').on('click', function(){為$("body").on("click", "#deleteimage", function(e){


我還刪除了:contentType: false, cache: false,processData:false,


感謝 Ghulam 朝著正確的方向努力


  $(document).ready(function(){

                $("body").on("click", "#deleteimage", function(e){

                    var image_id = parseInt($(this).parent().attr('id').replace('deleteform', ''));


                    console.log(image_id); // You can comment out this. Used for debugging.


                    e.preventDefault();

                    $.ajax({

                        url: "include_advert/advert_new_gun_delete_image.php",

                        type: "POST",

                        data:  {image_id: image_id },


                        success: function(data)

                        {

                            $('#imageareadiv').hide();

                            $("#targetLayer").html(data); // targetLayer is dynamic and is different for each record

                        },

                        error: function(xhr, status, error) {

                            alert(xhr.responseText);

                        }

                    });

                });

            });


查看完整回答
反對 回復 2021-09-05
?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

$(document).delegate('#deleteform button', 'click', function (e) {

代替

$("body").on("click", "#deleteform button", function(e){


查看完整回答
反對 回復 2021-09-05
  • 3 回答
  • 0 關注
  • 168 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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