javascript - 有没有办法将多个搜索功能组合成一个下拉选择?

标签 javascript html

我有多个搜索功能/文本框,我想将它们压缩成一个文本框+下拉选择。

http://jsfiddle.net/rhnt9vme/3/

<div>
  <form>
    <input type='text' id='searchOneInput' />
    <button type='submit' onclick='searchOne()' target="_blank">Search 1</button>
  </form>
</div><br>

<div>
  <form>
    <input type='text' id='searchTwoInput' />
    <button type='submit' onclick='searchTwo()' target="_blank">Search 2</button>
  </form>
</div><br>

<div>
  <form>
    <input type='text' id='searchThreeInput' />
    <button type='submit' onclick='searchThree()' target="_blank">Search 3</button>
  </form>
</div><br>

function searchOne(){
    var searchOneInput = document.getElementById('searchOneInput').value;
    window.open("http://one.com/search=" + searchOneInput);
}

function searchTwo(){
    var searchTwoInput = document.getElementById('searchTwoInput').value;
    window.open("http://two.com/search=" + searchTwoInput);
}

function searchThree(){
    var searchThreeInput = document.getElementById('searchThreeInput').value;
    window.open("http://three.com/search=" + searchThreeInput);
}

我想将它们组合成一个下拉选择单个文本框搜索,如下所示:

<form>
  <input type="text" id="userInput" />
  <select id="dropdown">
    <option value="">Search 1</option>
    <option value="">Search 2</option>
    <option value="">Search 3</option>
  </select>
  <button type="submit" onclick="">Search</button>
</form>

最佳答案

document.getElementById("searchForm").addEventListener("submit", function(ev) {
  ev.preventDefault(); // Comment out if not needed
  
  var dropdownURL = document.getElementById("dropdown").value;
  var searchInput = document.getElementById("userInput").value.trim();
  var address = dropdownURL + searchInput;
  
  console.log(address);
  window.open(address);
});
<form id="searchForm">  <!-- PS: add ID to form -->
  <input type="text" id="userInput">
  <select id="dropdown">
    <option value="http://one.com/?search=">Search 1</option>
    <option value="http://two.com/?search=">Search 2</option>
    <option value="http://three.com/?search=">Search 3</option>
  </select>
  <button type="submit">Search</button>
</form>

如果您需要一个组合 URI 字符串以及附加后缀查询参数,例如

https://example.com/?search=**USERINPUTHERE**&c=%2FDefault.asp

您可以在 not URI allowed characters 集中使用占位符,例如| ,并使用 String.prototype.replace() 将其替换为用户输入字符串

document.getElementById("searchForm").addEventListener("submit", function(ev) {
  ev.preventDefault(); // Comment out if not needed
  
  var dropdownURL = document.getElementById("dropdown").value;
  var searchInput = document.getElementById("userInput").value.trim();
  
  // Replace "|" with the user input
  var address = dropdownURL.replace(/\|/, searchInput);
  
  console.log(address);
  window.open(address);
});
<form id="searchForm">  <!-- PS: add ID to form -->
  <input type="text" id="userInput">
  <select id="dropdown">
    <option value="http://one.com/?search=|">Search 1</option>
    <option value="http://two.com/?search=|">Search 2</option>
    <option value="http://three.com/?search=|">Search 3</option>
    <option value="https://four.com/?search=|&c=%2FDefault.asp">Search 4</option>
  </select>
  <button type="submit">Search</button>
</form>

PS:由于查询参数的顺序并不重要,因此您始终可以定义字符串,例如: https://example.com/?c=%2FDefault.asp&search=无论如何,请使用第一个示例。

关于javascript - 有没有办法将多个搜索功能组合成一个下拉选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54223251/

相关文章:

php - Codeigniter 中的 HTML 格式的电子邮件

javascript - NodeJS 在函数完成之前响应

javascript - 使用 videojs 捕获无效源错误

javascript - 在 Chrome 中观察元素大小

javascript - 无法点击网页中的任何链接

html - 在浏览器中捕获媒体键事件

javascript - 如何在网页上找到按钮

javascript - 根据名称/ID 的一部分选择复选框

HTML 和正文没有 100% 宽度

html - 最小高度 100% 对内部 div 不起作用