简体   繁体   English

通过单击图像将文本插入输入字段

[英]Inserting text into input field by clicking on an image

I'm looking to do a "browse & insert image" kind of effect to put that images URL (all image info is saved in a database) into a input field for submission. 我正在寻找一种“浏览并插入图像”的效果,以将图像URL(所有图像信息都保存在数据库中)放入输入字段中进行提交。

Sort of like how in WordPress you can click browse, select an image, and then after you click okay it shows you an image in a text area, or the url in the input box. 就像在WordPress中一样,您可以单击浏览,选择图像,然后单击确定,它会在文本区域或输入框中的url中显示图像。

I'm not really sure what to search to learn how to do this. 我不确定要搜索什么以了解如何执行此操作。 I'm fairly new to javascript/jquery. 我是javascript / jquery的新手。 Any help is appreciated! 任何帮助表示赞赏!

Based on the request this will work: 根据请求,它将起作用:

HTML HTML

<input type="text" id="imgsrc" style="width:500px"/>
<div>
  <img src="https://placeimg.com/100/100/any"/>
</div>
<div>
  <img src="https://placeimg.com/100/100/arch"/>
</div>
<div>
  <img src="https://placeimg.com/100/100/tech"/>
</div>

jQuery jQuery的

$('img').on('click', function() {
    $('#imgsrc').val( $(this).attr('src') )
})

Fiddle Example 小提琴的例子

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

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