简体   繁体   English

如何为JavaScript函数创建文本字段?

[英]How can I make a text field for a JavaScript function?

I THINK i have managed to write a script. 我想我已经设法写了一个剧本。 I just can not make a textfield in HMTL to enter the missing data. 我只是无法在HMTL中创建文本字段来输入丢失的数据。 It it supposed to receive the keyword from a text field on submit click and navigate to the URL. 它应该在提交单击时从文本字段接收关键字,然后导航到URL。

I have tried multiple ways of forms and everything. 我尝试了多种形式和所有方式。 Should have installed VB.net and this would have been done in 5 min. 应该已经安装了VB.net,这将在5分钟内完成。

function urlMaker(keyword) {
  var base = "https://www.example.com/list.php?q=";
  var ending = "&dhd=1&hdd=low&dtt=list";
  var url;
  url = base + keyword + ending;
  window.location.assign(url);
}

In short words: I need to know how to create a HTML page with a textfield and a submit button. 简而言之:我需要知道如何创建带有文本字段和提交按钮的HTML页面。 When I submit it takes the text from the field and run the function and feeds it with the keyword from the textfield. 当我提交时,它从字段中获取文本并运行该函数,并使用来自文本字段中的关键字来输入。 When function has ran it redirects browser. 运行函数后,它将重定向浏览器。

I'm guessing you have a form like this. 我猜你有这样的形式。

Just attach a submit event-listener to it: 只需在其上附加一个submit事件监听器即可:

 document.querySelector("#search").addEventListener("submit", urlMaker) function urlMaker(event) { let keyword = document.querySelector("#keyword").value; let base = "https://www.example.com/list.php?q="; let ending = "&dhd=1&hdd=low&dtt=list"; let url; event.preventDefault(); url = base + keyword + ending; window.location.href = url; } 
 <form id="search"> <input type="text" id="keyword" /> <button type="submit">Search</button> </form> 

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

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