作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个搜索表单:
<form id="search" role="search" method="get" action="/">
<input id="search-text" type="text" placeholder="Search" name="s">
<input id="search-submit" type="submit" value="">
<div id="search-toggle"></div>
</form>
当点击 #search-toggle
div 时,它会变得可见,而当 #search-text
输入失去焦点时,它会隐藏。
$( '#search-toggle' ).on( 'click', function() {
$( '#search input, .social' ).add( this ).toggle();
$( '#search-text' ).focus();
});
$( '#search-text' ).focusout( function() {
$( '#search-submit, #search-toggle, .social' ).add( this ).toggle();
});
如果单击#search-submit
按钮,我如何确保在隐藏所有内容之前提交表单。
就目前情况而言,表单提交永远不会触发。
最佳答案
一种方法是在隐藏表单之前使用很少的超时,以便提交事件可以正确传播:
$('#search-text').focusout(function () {
var self = this;
timeout = setTimeout(function() {
$('#search-submit, #search-toggle, .social').add(self).toggle();
}, 200);
});
$('#search').on('submit', function() {
clearTimeout(timeout);
});
关于javascript - 在 .focusout() 之前触发表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27347008/
我是一名优秀的程序员,十分优秀!