用javascript改变图片源码的问题[重复]

问题描述 投票:0回答:1

我正试图改变一个图像 SRC 基于javascript中的一个变量。

我有以下的模式。

<div class="modal-body">
    <img src="https://www.w3schools.com/js/landscape.jpg" alt="image" width="400" name="image_modal" id="image_modal">
    <div class="form-group">
        <label>Image Name</label>
        <input type="text" name="image_name" id="image_name" class="form-control" />
    </div>
    <div class="form-group">
        <label>Image Description</label>
        <input type="text" name="image_description" id="image_description" class="form-control" />
    </div>
    <div class="form-group">
        <label>Path</label>
        <input type="text" name="path" id="path" class="form-control" />
    </div>  
</div>
<div class="modal-footer">
    <input type="hidden" name="image_id" id="image_id" value="" />
    <input type="submit" name="submit" class="btn btn-info" value="Edit" />
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>

然后我试着改变初始的虚拟 SRC 在JS中。

...
$.ajax({
   url:"edit.php",
   method:"post",
   data:{image_id:image_id},
   dataType:"json",
   success:function(data)
   {
    $('#imageModal').modal('show');
    $('#image_id').val(image_id);
    $('#image_name').val(data.image_name);
    $('#path').val(data.path);
    $('#image_description').val(data.image_description);
    document.getElementById("image_modal").src = val(data.path);
   }
});

我通过在模式中显示src URL(data.path)来检查它是否正确,并且它被正确输出。

但当我试图用 document.getElementById("image_modal").src = val(data.path);不显示图像,就像变量没有被正确解释一样。

javascript jquery image src
1个回答
0
投票

你的问题就在这里 val(data.path);

除非你已经定义了它。val() 是未定义的。您应该可以直接使用 data.path,假设你要找的是字符串。

用这个来代替

document.getElementById("image_modal").src = data.path;

© www.soinside.com 2019 - 2024. All rights reserved.