我用html5,css3和jquery製作了一個擴展的搜索欄。我的問題是,當瀏覽器自動填充任何內容並提交時,搜索欄會關閉而不是提交。擴展的搜索欄自動填充時關閉
這是我的代碼。
$('.searchbox-input').prop('required', 'true');
$(document).ready(function() {
var submitIcon = $('.searchbox-icon');
var inputBox = $('.searchbox-input');
var searchbox = $('.searchbox');
var isOpen = false;
submitIcon.click(function() {
if (!isOpen) {
searchbox.addClass('searchbox-open');
inputBox.focus();
isOpen = true;
} else {
searchbox.removeClass('searchbox-open');
inputBox.focusout();
isOpen = false;
}
});
$('#search').keyup(function() {
buttonUp();
});
submitIcon.mouseup(function() {
return false;
});
searchbox.mouseup(function() {
return false;
});
$(document).mouseup(function() {
if (isOpen) {
$('.searchbox-icon').css('display', 'block');
submitIcon.click();
}
});
});
function buttonUp() {
var inputVal = $('.searchbox-input').val();
inputVal = $.trim(inputVal).length;
if (inputVal !== 0) {
$('.searchbox-icon').css('display', 'none');
} else {
$('.searchbox-input').val('');
$('.searchbox-icon').css('display', 'block');
}
}
/* Searchbar */
.container {
width: 25%;
position: absolute;
top: 36px;
right: 200px;
}
.searchbox {
height: 40px;
width: 100%;
}
.searchbox-input {
position: absolute;
top: 0;
right: 0;
border: 0;
outline: none;
background: #313132;
width: 80%;
height: 40px;
margin: 0;
padding-left: 20px;
font-size: 17px;
color: #efefef;
outline-style:none;
box-shadow:none;
border-color:transparent;
font-family: Comfortaa, Calibri, Candara, Segoe, "Segoe UI", Optima, Arial, sans-serif;
}
.searchbox-input:focus {
outline-style:none;
box-shadow:none;
border-color:transparent;
}
.searchbox-input::-webkit-input-placeholder {
color: #808080;
}
.searchbox-input:-moz-placeholder {
color: #808080;
}
.searchbox-input::-moz-placeholder {
color: #808080;
}
.searchbox-input:-ms-input-placeholder {
color: #808080;
}
.searchbox-icon, .searchbox-submit {
width: 40px;
height: 40px;
display: block;
position: absolute;
top: 0;
right: 0;
padding: 0;
margin: 0;
border: 0;
outline: none;
line-height: 40px;
text-align: center;
cursor: pointer;
color: #313132;
font-size: 20px;
background: #4BC599;
}
.searchbox {
position: relative;
min-width: 40px;
width: 0%;
height: 40px;
float: right;
overflow: hidden;
-webkit-transition: width 0.1s;
-moz-transition: width 0.1s;
-ms-transition: width 0.1s;
-0-transition: width 0.1s;
transition: width 0.1s;
}
.searchbox-open {
width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<form action="" class="searchbox">
<input type="text" class="searchbox-input" placeholder="Enter search term" name="search" id="search">
<button type="submit" class="searchbox-submit" value=""><i class="fa fa-search"></i>
</button> \t <span class="searchbox-icon"><i class="fa fa-search"></i></span>
</form>
</div>
我希望你能幫助我。
問候和感謝
你的小提琴就是缺少jQuery的! > http://jsfiddle.net/g1qtaoy9/2/ – mahe 2014-12-03 12:59:01
噢,新的,謝謝! – Fluqz 2014-12-03 13:03:46