简体   繁体   English

如何从模板文字中获取输入值并将其分配给变量?

[英]How to get the input value from a template literal and assign it to a variable?

I have tried to get the class or id attribute of the input field from this template literal and after that, I will like to assign it to a variable so that I can use the add Event Listener method to asset the value of the user's input when they enter letter in the input filed.我试图从这个模板文字中获取输入字段的 class 或 id 属性,之后,我想将它分配给一个变量,以便我可以使用添加事件监听器方法来评估用户输入的值他们在输入字段中输入字母。 But I am confused about how to implement this.但我对如何实现这一点感到困惑。 I will be pleased if someone help can help me out.如果有人可以帮助我,我会很高兴。

// search bar function
const searchBar = () => {
  const createSearchForm = `
    <form action="#" method="get">
      <input type="search" id="search-input" class="search-input" placeholder="Search...">
      <input type="submit" value="&#x1F50D;" id="search-submit" class="search-submit">
    </form>`;
  searchParent.insertAdjacentHTML('afterbegin', createSearchForm)
}
searchBar();

Use DOMParser() :使用DOMParser()

 const createSearchForm = ` <form action="#" method="get"> <input type="search" id="search-input" class="search-input" placeholder="Search..."> <input type="submit" value="&#x1F50D;" id="search-submit" class="search-submit"> </form>`; var val = new DOMParser().parseFromString(createSearchForm,"text/html").querySelector("input[type='submit']").value; console.log(val);

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

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