简体   繁体   English

使用javascript中的图像src填充输入字段

[英]Populate an input field with an image src in javascript

I have an XHR response that returns images. 我有一个返回图像的XHR响应。 I have my function in order to show the images. 我有我的功能,以显示图像。 I am combining JQuery and JS in the same code snippet. 我将JQuery和JS结合在同一个代码片段中。 So far all is working well: 到目前为止一切运作良好:

function resultat(o){  
          var leselements = o.query.results.bossresponse.images.results.result;
          var output = '';  
          var no_items = leselements.length;  
          for(var i=0;i<no_items;i++){  
            var lien = leselements[i].url;

            //place image urls in img src  
            output += "<img src='" + lien + "' class='imgs'>";  
          }  
          // Place images in div tag  
          document.getElementById('results').innerHTML = output;}

But I would like to allow users to click an image and then populate an input field ('#imageurl') with the clicked image src. 但我希望允许用户单击图像,然后使用单击的图像src填充输入字段('#imageurl')。 Here is what I tried but it does not work. 这是我尝试但它不起作用。

$('.imgs img').click(function(){
            $('#imageurl').val() = "";
            var source = $(this).attr('src');
            $('#imageurl').val() = source;    
          });

Any help will be greatly appreciated. 任何帮助将不胜感激。 TIA. TIA。

Using .val() in this way will just return the current value of #imageurl. 以这种方式使用.val()将只返回#imageurl的当前值。

$('#imageurl').val()

.val is a function call that works as a getter and a setter. .val是一个函数调用,用作getter和setter。

To set the value, try this: 要设置该值,请尝试以下操作:

$('#imageurl').val(source);
$('#imageurl').val("");
// ...
$('#imageurl').val(source);

See the documentation . 请参阅文档

Try this: 尝试这个:

$('img.imgs').click(function(){
   var src = $(this).attr('src');
   $('#imageurl').val(src);  
});

If the image will be rendered after the attachment of the event handler use this: 如果在附加事件处理程序后将呈现图像,请使用以下命令:

$('img.imgs').live('click', function(){
   var src = $(this).attr('src');
   $('#imageurl').val(src);  
});

Thank you guys for your prompt answers. 谢谢你们的快速回答。 I tried all of them but they did not work for me. 我尝试了所有这些,但他们没有为我工作。 I then asked a friend and we finally found a way to make it work. 然后我问了一位朋友,我们终于找到了一种让它成功的方法。 Probably not the best or professional way but it works. 可能不是最好的或专业的方式,但它的工作原理。 Here is the solution if ever anyone needs it. 如果有人需要,这是解决方案。

function resultat(o){  
          var leselements = o.query.results.bossresponse.images.results.result;
          var output = '';  
          var no_items = leselements.length;  
          for(var i=0;i<no_items;i++){  
            var link = leselements[i].url;

//Place urls in image src and pass in 'link' parameter to the getsrc function 
            output += "<img src='" + link + "' onclick='getsrc(\""+link+"\")'>";  
          }  
          // Place images in div tag  
          document.getElementById('results').innerHTML = output;


        }

         function getsrc (link) {
            //console.log($(this));
            $('#imageurl').val("");
           // var source = $(this).attr('src');
            //place imageurl value by passing in the link parameter. 
            $('#imageurl').val(link);

          }

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

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