Html CSS搜索栏:单击图标时没有任何反应

问题描述 投票:0回答:4

我正在使用一个网站模板,其中包含一个带有图标的搜索表单。 Search icon

输入值并且用户单击“Enter”时,将执行表单。但是,单击搜索图标时,没有任何反应。

Nothing happens when clicking the icon

该图标是通过令人敬畏的字体库http://fontawesome.io/创建的,并通过css添加

content: '\f002';

这是代码的小提琴:https://jsfiddle.net/BamBamm/df2gzkbb/10/

我怎样才能解决这个问题?谢谢。

#search form {
	text-decoration: none;
	position: relative;
}

#search form:before {
	-moz-osx-font-smoothing: grayscale;
	-webkit-font-smoothing: antialiased;
	font-family: FontAwesome;
	font-style: normal;
	font-weight: normal;
	text-transform: none !important;
}

#search form:before {
	-moz-transform: scaleX(-1);
	-webkit-transform: scaleX(-1);
	-ms-transform: scaleX(-1);
	transform: scaleX(-1);
	color: #7f888f;
	content: '\f002';
	cursor: default;
	display: block;
	font-size: 1.5em;
	height: 2em;
	line-height: 2em;
	opacity: 0.325;
	position: absolute;
	right: 0;
	text-align: center;
	top: 0;
	width: 2em;
}
    
input[type="text"] {
    height: 2.75em;
}

input[type="text"] {
    -moz-appearance: none;
    -webkit-appearance: none;
    -ms-appearance: none;
    appearance: none;
    background: #ffffff;
    border-radius: 0.375em;
    border: none;
    border: solid 1px rgba(210, 215, 217, 0.75);
    color: inherit;
    display: block;
    outline: 0;
    padding: 0 1em;
    text-decoration: none;
    width: 100%;
}

body, input, select, textarea {
    color: #7f888f;
    font-family: "Open Sans", sans-serif;
    font-size: 13pt;
    font-weight: 400;
    line-height: 1.65;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/3.1.0/css/font-awesome.min.css" rel="stylesheet" />
<div id="search" class="alt">
 <form action="javascript:alert('Inner Function performed!');">
	  <input type="text" name="search_query" value="defaultValue"/>
  </form>
</div>
html css font-awesome
4个回答
4
投票

您可以在提交按钮上添加图标

请检查该代码:

<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head>

<div class="container">
<div class="col-md-3">
  <form class="navbar-form" role="search" action="javascript:alert('Inner Function performed!');">
    <div class="input-group add-on">
      <input class="form-control" placeholder="Search" name="srch-term" id="srch-term" type="text">
      <div class="input-group-btn">
        <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
      </div>
    </div>
  </form>
  				
</div>
</div>

1
投票

您可以添加具有相同图标的提交按钮,而不是将图标放在表单中的伪元素上:

https://jsfiddle.net/df2gzkbb/12/

虽然这有效,但我建议你不要改变行为。在我看来通过点击图标发送表单将是意外行为,因此会恶化用户体验。

#search form {
  text-decoration: none;
  position: relative;
}

#search form button {
  font-size: 0;
  position: absolute;
  right: 0;
  top: 8px;
  background-color: transparent;
  border: 0;
  appearance: none;
}

#search form button:before {
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;
  font-family: FontAwesome;
  font-style: normal;
  font-weight: normal;
  text-transform: none !important;
  -moz-transform: scaleX(-1);
  -webkit-transform: scaleX(-1);
  -ms-transform: scaleX(-1);
  transform: scaleX(-1);
  color: #7f888f;
  content: '\f002';
  cursor: default;
  display: block;
  font-size: 1.5rem;
  height: 2rem;
  line-height: 2rem;
  opacity: 0.325;
  text-align: center;
  width: 2rem;
}

input[type="text"] {
  height: 2.75em;
}

input[type="text"] {
  -moz-appearance: none;
  -webkit-appearance: none;
  -ms-appearance: none;
  appearance: none;
  background: #ffffff;
  border-radius: 0.375em;
  border: none;
  border: solid 1px rgba(210, 215, 217, 0.75);
  color: inherit;
  display: block;
  outline: 0;
  padding: 0 1em;
  text-decoration: none;
  width: 100%;
}

body,
input,
select,
textarea {
  color: #7f888f;
  font-family: "Open Sans", sans-serif;
  font-size: 13pt;
  font-weight: 400;
  line-height: 1.65;
}
<link rel="stylesheet" href="https://opensource.keycdn.com/fontawesome/4.7.0/font-awesome.min.css " />
<div id="search" class="alt">
  <form action="javascript:alert('Inner Function performed!');">
    <input type="text" name="search_query" value="defaultValue" />
    <button type="submit">Search</button>
  </form>
</div>

1
投票

向DOM添加一个范围并向其添加一个事件:

$('#glyphicon').click(function(){
  $('#myform').submit()
})
#glyphicon {
    position: relative;
    left: -25px;
    top: 6px;
    background-image: url(http://wfarm4.dataknet.com/static/resources/icons/set110/37275f5a.png);
    background-size : 100% 100%;
    width: 20px;
    height: 20px;
    display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="search" class="alt">
 <form id="myform" action="javascript:alert('Inner Function performed!');">
	  <input type="text" name="search_query" value="defaultValue"/>
    <span id="glyphicon">
  
    </span>
  </form>
</div>

1
投票

设置搜索按钮的onclick事件。代码应该是这样的

<div onclick="myFunction()">
       <i class="fa fa-minus-circle"></i>
</div>

表单将Enter键击作为提交。您必须为鼠标单击事件设置事件。

© www.soinside.com 2019 - 2024. All rights reserved.