简体   繁体   English

有问题被选中img src并在文本字段中显示

[英]having a issue getting selected img src and displaying in text field

Can anyone please help me with a issue am having with javascript and on selection?, first of all what am trying to do is when I select a image from a list of images I want to get the image src and store it to a var that I can use else were now the problem am having when I select image i get the following src portraits/00000/00008.jpg now I need to remove the portraits and both / and the .jpg from the src so i just got the folder ID and image ID and add them to a fieldtext box in realtime without refreshing page. 任何人都可以帮我解决一个与javascript和选择有关的问题吗?首先我要做的是当我从图像列表中选择一个图像时我想要获取图像src并将其存储到var中我可以使用其他当我选择图像时遇到的问题我得到以下src肖像/ 00000 / 00008.jpg现在我需要从src中删除肖像和/和.jpg这样我才得到文件夹ID和图像ID,并将它们实时添加到字段文本框中,而无需刷新页面。

<script type="text/javascript">
    $(document).ready(function() {
      $('.portrait').click(function(){
        var idimg = $(this).attr('id');
        var srcimg = $(this).attr('src');

        alert(srcimg); // shows debug

      });
    });
    </script>

<input name="sel-avatar" type="textfield" id="avatar" value="IMAGEID" />
<input name="sel-avatardir" type="textfield" id="avatardir" value="FOLDERID"

image display is 图像显示是

<img id=\"portrait\" class=\"portrait\" src=\"{$files[$pos]}\" width=\"88\" height=\"88\" border=\"1\" />

can anyone help ? 有人可以帮忙吗?

Could be this you're looking for? 您要找的这个吗?

<script type="text/javascript">
    $(document).ready(function() {
      $('.portrait').click(function(){
        var idimg = $(this).attr('id');
        var srcimg = $(this).attr('src');

        var splitSrc = srcimg.split("/");

        var folder = splitSrc[1];
        var splitFile = splitSrc[2].split(".");
        var fileId = splitFile[0];
        document.getElementById("avatar").value=fileId;
        document.getElementById("avatardir").value=folder;

      });
    });
</script>

if you are sure you would get in the same format ie folder ID, then image ID, image name you could use the split function to get the javascript array and fetch the details from the array.. 如果您确定将使用相同的格式,即文件夹ID,图像ID,图像名称,则可以使用split函数获取javascript数组并从该数组中获取详细信息。

<script type="text/javascript">
"portraits/00000/00008.jpg".split(/[\/]+/)
</script>

Array [ "portraits", "00000", "00008.jpg" ] 数组[“ portraits”,“ 00000”,“ 00008.jpg”]

Try this: 尝试这个:

var ary = srcimg.split(/\/|\./g);

var folder = ary[1];

var file = ary[2];

The assumption here is that you'll have one folder and there aren't any periods in the folder or file name, except before the file extension. 这里的假设是你将有一个文件夹,文件夹或文件名中没有任何句点,除了文件扩展名之外。 If there are multiple folders, we have to change the assignment slightly, but the split statement is the same. 如果有多个文件夹,我们必须稍稍更改分配,但是split语句是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM