首页 > 解决方案 > 有什么办法可以在一个表单中使用多个输入?

问题描述

我在做我的项目时遇到了麻烦。在我的项目中,我在 HTML 中进行谷歌高级搜索,我需要 4 个字段。这 4 个字段是用户输入,其中指定了他们在搜索中想要的边界。当用户点击 google 搜索时,它会在 javascript 中执行 result() 操作。结果从文本框中获取这些输入并将其存储为变量并调用警报。我的问题是如何获取我在 javascript 中形成的字符串并将其用作 google 搜索的表单响应。

function result() {
    var atw = document.getElementById("aTW").value;
    var ewp = document.getElementById("eWP").value;
    var tw = document.getElementById("tW").value;
    var ntw = document.getElementById("nTW").value;
    var string = atw + " " + tw + " " + "\"" + ewp + "\"" + " " + "-" + ntw;
    alert(string);

}
      
<h3>Advanced Search</h3>
<form action="https://www.google.com/search">
   <p>     
      <label>All these words</label>
      <input type = "text"
         id = "aTW"
         value = ""/>
   </p>
   <p>
      <label>this exact word or phrase:</label>
      <input type = "text"
         id = "eWP"
         value = "" />
   </p>
   <p>
      <label>any of these words:</label>
      <input type = "text"
         id = "tW"
         value = "" />
   </p>
   <p>
      <label>none of these words:</label>
      <input type = "text"
         id = "nTW"
         value = "" />
   </p>
   <input type="text" name ="q" id = "inputs" placeholder="Search">     
   <input onclick = "result()" type="submit" value="Google Search">
</form>
       

标签: javascripthtmlcss

解决方案


how can I take the string I formed in javascript and use it as the form response for the google search.

将您的谷歌搜索查询形成为您的字符串,然后将查询发送到谷歌搜索页面......

function  googleSearch(query){
  url = 'http://www.google.com/search?q=' + query;
  window.open(url, '_blank');
}

在你的 result() 函数中调用你的函数进行谷歌搜索......

function result() {
  var atw = document.getElementById("aTW").value;
  var ewp = document.getElementById("eWP").value;
  var tw = document.getElementById("tW").value;
  var ntw = document.getElementById("nTW").value;
  var string = atw + " " + tw + " " + "\"" + ewp + "\"" + " " + "-" + ntw;
  alert(string);
  googleSearch(string);
}

https://jsfiddle.net/ov04hr12/


推荐阅读