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

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

使用$.each循環創建多個div,不起作用

使用$.each循環創建多個div,不起作用

PHP
子衿沉夜 2023-08-11 16:33:16
我的 php 頁面設置如下:<?php    if(isset($_POST['submit'])){        // Validate results and if correct insert into the DB        // Else add the respective errors        $data[0]['error_1'] = 'Error_1_text';        $data[0]['error_2'] = 'Error_2_text';        /*        .        .        .        */        $data[0]['error_n'] = 'Error_n_text';        print_r(json_encode($data[0]));    }?>這是以下 AJAX 請求發送 POST 請求的位置。如果輸入的表單有效,則將其插入數據庫,否則如果有任何錯誤,則所有錯誤都會附加到數組索引中,$data[0]最后數組是json_encoded, 并print_r()編輯。<body>    <div class="ssss"></div></body><script>    $.ajax({        url: "http://localhost/path/to/above/file",        method: "POST",        data: {            submit: 'yes',            form_data_0: 'form_text',            form_data_1: 'form_text',            form_data_2: 'form_text'        },        success: function(data){            // Code to check if errors were present            result = JSON.parse(data);            if (result.hasOwnProperty('0')) {                $.each(result['0'], function(key, error) {                    let x = 0;                    // Display each error into a modal                    $("div#ssss").html('<div id="dmessage_' + x + '" class = "modal" style="display:block"><p id = "pmessage_' + x + '">' + error + '</p></div>');                    x++;                });            }        }    });</script>AJAX 請求成功后,data為JSON.parse()d 并且該結果被分配給變量result。然后我們檢查表單的后處理過程中是否有任何錯誤。使用if (result.hasOwnProperty('0')),如果是這樣,目的是顯示<p>error</p>帶有動態 id 的 a 中的每個錯誤,并包含在<div>帶有動態 id 的 a 中。為了實現這一點,我創建了一個$.each()循環并啟動了一個變量x = 0?,F在我們遍歷$.each()循環的內容,我們打算顯示 一個 ,其中<div></div>包含代碼中顯示的內容以及errorPHP 從后端發送過來的文本以及dmessage_與 的當前值連接的id x。完成后,我們將遞增x并再次循環以處理下一個錯誤。然而發生的情況是,無論 PHP 發送了多少錯誤,都只顯示 1 個錯誤框。我通過在語句中添加以下代碼來確認這一點if():let array = [];$.each(result['0'], function(key, error) {    // Display each error into a modal});array[x] = array.push(error);console.log(array); // Displays all the errors correctly inside the console
查看完整描述

2 回答

?
呼如林

TA貢獻1798條經驗 獲得超3個贊

我知道你有解決方案,但我會發布我的答案

第一: echo json_encode

第四:根本不需要使用,x可以直接使用key

第五:可以直接使用dataType: 'json'代替JSON.parse(data)

第六:更改.html(.append(

考慮以上所有內容,那么您的代碼應該如下所示

php

<?php

    if(isset($_POST['submit'])){

        // Validate results and if correct insert into the DB

        // Else add the respective errors

        $data['error']['error_1'] = 'Error_1_text'; // change all `$data[0]` to `$data['error']` it will be much easier 

        $data['error']['error_2'] = 'Error_2_text';

        /*

        .

        .

        .

        */

        $data['error']['error_n'] = 'Error_n_text';

        echo(json_encode($data));  // <<<< just $data here

    }

?>

JS


<body>

    <div class="ssss"></div>

</body>

<script>

    $.ajax({

        url: "http://localhost/path/to/above/file",

        method: "POST",

        dataType : 'json',  //<<<<< here

        data: {

            submit: 'yes',

            form_data_0: 'form_text',

            form_data_1: 'form_text',

            form_data_2: 'form_text'

        },

        success: function(data){

            // Code to check if errors were present

            if (data.error) {   //<<<<< here

                $.each(data.error, function(key, error) {

                    // Display each error into a modal

                    $("div#ssss").append('<div id="dmessage_' + key + '" class = "modal" style="display:block"><p id = "pmessage_' + key + '">' + error + '</p></div>');

                });

            }else{

               $("div#ssss").html('');

            }

        }

    });

</script>


查看完整回答
反對 回復 2023-08-11
?
暮色呼如

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

這解決了我的問題:


<script>

    $.ajax({

        url: "http://localhost/path/to/above/file",

        method: "POST",

        data: {

            submit: 'yes',

            form_data_0: 'form_text',

            form_data_1: 'form_text',

            form_data_2: 'form_text'

        },

        success: function(data){

            // Code to check if errors were present

            result = JSON.parse(data);

            let x = 0;

            if (result.hasOwnProperty('0')) {

                $.each(result['0'], function(key, error) {

                    // Display each error into a modal

                    $("div#ssss").append('<div id="dmessage_' + x + '" class = "modal" style="display:block"><p id = "pmessage_' + x + '">' + error + '</p></div>');

                });

            }

        }

    });

</script>


查看完整回答
反對 回復 2023-08-11
  • 2 回答
  • 0 關注
  • 155 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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