input type=“search” 实现搜索框。

发布时间:2024-12-22 16:33

使用智能购物APP,可以实现语音搜索和识别 #生活知识# #购物技巧# #智能购物#

欲实现一个文字搜索的功能,要求输入时,键盘回车按钮提示显示为“搜索”。效果如下:

input type=text并不能达到这种效果,google了一下,html5 增加的type=search可以做到(但需要input type=search外面包上一层带action属性的form)。

<div class="search-input-wrap clearfix">

<div class="form-input-wrap f-l">

<form action="" class="input-kw-form">

<input type="search" autocomplete="off" name="baike-search" placeholder="请输入关键词" class="input-kw">

</form>

<i class="iconfont if-message"></i>

<i class="iconfont if-close"></i>

</div>

<i class="search-cancel f-l">取消</i>

</div>

但type=search会有许多默认样式和行为,这次开发遇到的有:

会默认下拉框显示搜索历史记录;

输入时自动弹出“x”,“x”的样式在不同手机上,样式不同;

IOS 手机(测试时为iphone6 ios10)上输入框为椭圆形.

但我们希望样式按照我们自定义的样式显示,并且各个手机上能统一。

于是几经google,得到答案:

设置input autocomplete="off"去掉弹出的下拉框;

将默认的“x”隐藏掉:

input[type="search"]::-webkit-search-cancel-button{

display: none;

}

针对ios 设置样式, 去除ios下input 椭圆形:

-webkit-appearance: none;

同时别忘记,如果提交搜索时想使用ajax,那么可以阻止表单的默认行为:

container.on('submit', '.input-kw-form', function(event){

event.preventDefault();

})

'

注意:container是页面最外层容器,其实就是绑定事件,不绑定在container上也可以的。

如果想用ajax提交请求的话,可以用以下代码实现。

$("#searchForm").on("submit",function(){

getList();

$('#inp_guardianPhone').blur();

return false;

})

'

转载:https://segmentfault.com/a/1190000007765742

网址:input type=“search” 实现搜索框。 https://www.yuejiaxmz.com/news/view/540141

相关内容

简单代码autocomplete=“off”实现禁用input输入框的自动提示功能
巧用两个type=range input实现区域范围选择 « 张鑫旭
input type=“time”
利用css3修改input[type=radio]样式
input标签的type为select、radio、checkbox的使用
JS中给input框赋值
数组传值input type=“checked”
CSS小技巧——去除input[type=number]的默认样式
修改 input type=file 的样式的最简单方法
HTML input type=file文件选择表单元素二三事 « 张鑫旭

随便看看