我有以下使用输入用户名的 HTML 进行搜索,例如“用户”
然后当点击搜索栏时,页面将转到http://localhost/searchuser/user
例如,使用输入字段中的 GET 方法。我现在遇到了这个问题。
我的实现是<form action="/searchuser" + username>
对吗?
代码
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
* {box-sizing: border-box;}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #e9e9e9;
}
.topnav a {
float: left;
display: block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #2196F3;
color: white;
}
.topnav .search-container {
float: right;
}
.topnav input[type=text] {
padding: 6px;
margin-top: 8px;
font-size: 17px;
border: none;
}
.topnav .search-container button {
float: right;
padding: 6px 10px;
margin-top: 8px;
margin-right: 16px;
background: #ddd;
font-size: 17px;
border: none;
cursor: pointer;
}
.topnav .search-container button:hover {
background: #ccc;
}
@media screen and (max-width: 600px) {
.topnav .search-container {
float: none;
}
.topnav a, .topnav input[type=text], .topnav .search-container button {
float: none;
display: block;
text-align: left;
width: 100%;
margin: 0;
padding: 14px;
}
.topnav input[type=text] {
border: 1px solid #ccc;
}
}
</style>
</head>
<body>
<div class="search-container">
<form action="/searchuser" + username>
<input type="text" placeholder="Username.." name="username">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
</div>
</div>
<div style="padding-left:16px">
<h2>Responsive Search Bar</h2>
<p>Navigation bar with a search box and a submit button inside of it.</p>
<p>Resize the browser window to see the responsive effect.</p>
</div>
</body>
</html>
最佳答案
不,不会。只有像 onclick
这样的事件属性可以采用内联 JavaScript 代码。
您可以为文本输入的blur
事件添加一个监听器,并在监听器中根据文本输入更改表单的action
。
<form action="" id="form">
<input type="text" placeholder="Username.." onchange="document.getElementById('form').action = '/searchuser/' + this.value;" name="username">
<button type="submit"><i class="fa fa-search"></i></button>
</form>
另外,即使是事件属性,也要把所有的代码都用“.”包起来。
action="/searchuser" + username
从另一个 Angular 来看,它不是一个有效的 html 属性。
关于使用 GET 方法的 HTML CSS 搜索 BAR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48738091/