javascript - 有没有办法将多个搜索功能组合成一个下拉选择?
问题描述
我有多个搜索功能/文本框,我想将它们压缩成一个文本框 + 下拉选择。
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>
解决方案
- 为您的表单设置一个 ID 并使用方法监听
"submit"
事件Element.addEventListener()
<option>
在svalue
属性中设置相应的网站 URL- 不要忘记修剪用户输入,以防意外换行
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
比您可以在一组不允许使用 URI的字符中使用占位符,例如|
,并使用用户输入字符串替换它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=
并使用第一个示例。
推荐阅读
- r - 使用 cleanNLP 和 stanford-corenlp 后端注释西班牙语句子时的编码问题
- c++ - 如何在 geos-C++ 中执行 unaryunion 函数
- python-3.x - 如何在 Python3 中将 UTC 日期字符串转换为时区感知日期时间对象?
- gis - Netlogo GIS 不支持的形状类型文件
- html - SVG 元素在 Chrome 浏览器中居中对齐,但应向右对齐
- java - 改变方法内部的局部变量,方法执行时变量不变
- angular - 将自定义组件指令绑定到 for 循环中的对象
- node.js - 如何比较数组mongodb查询
- python - 无法确定父/子表之间的连接条件,设置foreign_keys
- visual-studio-code - IDE 对 TypeScript 内部的支持