图片标签不接受src

I am trying to make an image take a value in as a source, after the image tag (and a related radio button) has been created using JavaScript. I have discerned the following from testing and alert outputs:

  • If the image src is provided at the creation of the image tag using an exact filepath, it will show the image correctly (e.g. src='images/0.jpg'). However, this is not helpful since I need it to work for any given image, not a specific one.

  • If the image src is provided at the creation of the image tag using a variable containing a filepath, it fails to generate the image tag or the radio button at all (e.g. src='" + result + '").

NOTE: The last example is not present in the code below. The result was found by moving the '$.post' section to the line directly under the 'out +=' line within the for loop.

  • If the image src is left blank at the creation of the image tag, the image tag and radio button are created, though the image is blank as expected. If I then try to use 'getElementByID(imgID).src' to change the image source after this, it fails to do anything. ('imgID' here is an example, not what the code says).

On top of the above, using alerts and dumping info into divs indicate that the comicID is being correctly posted, and the filepath of the image src is definitely being found, and is being copied into the variable 'result' correctly, even one line before the creation of the tag or the attempt to edit it using 'getElementById'.

At this point I'm stumped, I don't know what could logically be stopping the src from reading in.

--

Javascript:

<script>
// Loads the user's comic list from the database.
        function loadComic()
        {
        var xmlhttp = new XMLHttpRequest();
        var getID = '<?php echo $_SESSION["userID"]; ?>';
        var url = "loadCom.php?userID="+getID;


        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                loadComicJSON(xmlhttp.responseText);
            }
        }

        xmlhttp.open("GET", url, true);
        xmlhttp.send();

        }

        // JSON parsing for 'loadComic'.
        function loadComicJSON(response)
        {
            var arr = JSON.parse(response);
            var i;
            var out = "";

            document.getElementById("loadList").innerHTML="";

            if (arr.length == 0)
            {
                //Irrelevant manipulation of HTML.
            }
            else
            {   
                out+="<br>";

                for(i = 0; i < arr.length; i++)
                {
                    out += "<hr><br><img name = '" + ('cm' + arr[i].comicID) + "' id='" + ('com' + arr[i].comicID) + "' onclick='resizeThumb(this)' height='100px;' src='' ><input name='comicList' type='radio' id='" + arr[i].comicID + "' value='" + arr[i].comicID + "'>" + arr[i].comicName + " </option><br><br>";                        
                }


                document.getElementById("loadList").innerHTML=out;
                for(j=0; j< arr.length; j++)
                {
                    tempID = (arr[j].comicID);
                    $.post("getCover.php", {comicID:tempID}, function(result)
                    {
                        document.getElementById("loadList").innerHTML+="<p>"+result+"</p>";
                        document.getElementById("com"+arr[j].comicID).src = result;
                    }
                    );

                }

            }

        }

    </script>

PHP (getCover.php):

<?php
if (isset($_POST["comicID"]))
{
    include_once('includes/conn.inc.php');
    $checkID = $_POST["comicID"];

    $query = ("SELECT pageLocation FROM page WHERE comicID = '$checkID' ORDER BY pageNum");
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_assoc($result);
    print_r($row["pageLocation"]);
}
else
{   
    $checkID = null;
    echo "Error. No comic found.";
}

?>

To my knowledge, loadList.php is working perfectly, so I didn't list its code to keep things relevant.

I copied your code and tweaked it a little so I could run it without the web services and it works great. Here is the HTML page I created:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
<script>

    // JSON parsing for 'loadComic'.
    function loadComicJSON()
    {
        var arr = [{comicID: 1},{comicID: 2},{comicID: 3}];
        var result = "monkey.jpeg";
        var i;
        var out = "";

        document.getElementById("loadList").innerHTML="";

        if (arr.length == 0)
        {
            //Irrelevant manipulation of HTML.
        }
        else
        {
            out+="<br>";

            for(i = 0; i < arr.length; i++)
            {
                out += "<hr><br><img name = '" + ('cm' + arr[i].comicID) + "' id='" + ('com' + arr[i].comicID) + "' onclick='resizeThumb(this)' height='100px;' src='' ><input name='comicList' type='radio' id='" + arr[i].comicID + "' value='" + arr[i].comicID + "'>" + arr[i].comicName + " </option><br><br>";
            }


            document.getElementById("loadList").innerHTML=out;
            for(j=0; j< arr.length; j++)
            {
                var imgSrc;
                tempID = (arr[j].comicID);
                document.getElementById("loadList").innerHTML+="<p>"+result+"</p>";
                document.getElementById("com"+arr[j].comicID).src = result;

            }

        }

    }

</script>
</head>
<body>
<div id="loadList"></div>
<button onclick="loadComicJSON()">Try it</button>

</body>
</html>

As you can see, I created an array of JSON objects that hold the comicID and am statically creating the image as 'monkey.jpeg'.

The code works so there is either an issue with the 'response' that you pass into your loadComicJSON method or the result from your POST method.

Add a couple of console.log statements and look at the two values I mentioned and you will likely see the issue.

Solved the issue myself. It turned out that the $.post needed to be $.get and that it needed to technically be outside of a loop (i.e. in its own function) to work properly. Works fine now after that and a couple minor tweaks.