简体   繁体   English

如何使用用户输入值更新图像src属性

[英]How to update image src attribute using user input value

I am building a simple photo editor app using imgix, and I want the image src attribute to be updated when a user input fields receive values. 我正在使用imgix构建一个简单的照片编辑器应用程序,并且我希望在用户输入字段接收值时更新image src属性。 Is there a way to append my new parameters onto the end of the src url? 有没有办法将我的新参数附加到src url的末尾?

I have been able to use template literals to create a new url, however I haven't been able to figure out how or if I can update the image src as the user types. 我已经能够使用模板文字来创建新的url,但是我无法弄清楚如何或是否可以根据用户类型更新图像src。 Or if the only way to do this would be submitting the changes and show the new image on page reload. 或者,如果唯一的方法是提交更改并在页面重新加载时显示新图像。 I have tried reassigning image.src to the new url, but that hasn't worked. 我尝试将image.src重新分配给新的url,但这没有用。

HTML:

<form id="form" class="input-container">
  <input type="text" placeholder="Enter Text" id="title"/>
  <input type="text" placeholder="Enter Hexcode Color" id="overlay" />
  <input type="submit" value="Apply Changes" id="edit-submit">
</form>


JavaScript:
let form = document.getElementById("form");
let titleInput = document.getElementById("title");
let encodedTitle = encodeURI(titleInput);
let overlayColor = document.getElementById("overlay");
let image = document.getElementById("image");
let timeout = null;

form.onkeyup = function(e) {
  let encodedTitle = "&txt=" + encodeURI(titleInput.value);
  let newColor = "&blend=" + overlayColor.value;
  let url = new URL(`${image.src}`);
  url = `${url}${encodedTitle}${newColor}`;
  console.log(url);
  document.getElementById("url-result").value = url;
  image.src = url;
};

I am able to grab the values but I haven't figured out what I'm needing to do to apply it to the img src url. 我可以获取这些值,但是我还没有弄清楚将其应用于img src url所需要做的事情。

Your code changes the URL of the image, but the URL gets longer and longer with each keyup. 您的代码更改了图像的URL,但是每次键入键时URL都变得越来越长。

Look at this snippet - maybe it helps. 看一下这段代码-也许有帮助。

 let form = document.getElementById("form"); let titleInput = document.getElementById("title"); let encodedTitle = encodeURI(titleInput); let overlayColor = document.getElementById("overlay"); let image = document.getElementById("image"); let timeout = null; // URL base for your image - so it doesn't get longer-and-longer // with each keyup let baseURL = image.src // just a display element, so you can see the URL created let display = document.getElementById('new-url') form.onkeyup = function(e) { const newURL = `${baseURL}&txt=${encodeURI(titleInput.value)}&blend=${overlayColor.value}` document.getElementById("url-result").value = newURL; image.src = newURL display.textContent = newURL }; 
 img { width: 100px; height: 100px; } 
 <img id="image" src=""><br /> <label for="url-result">URL Result: <input type="text" id="url-result"> </label> <div>Display new URL: <span id="new-url"></span></div> <form id="form" class="input-container"> <input type="text" placeholder="Enter Text" id="title" /> <input type="text" placeholder="Enter Hexcode Color" id="overlay" /> <input type="submit" value="Apply Changes" id="edit-submit"> </form> 

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

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