我有一个带有高级选项的表单。高级选项位于div(隐藏)中。当“搜索”输入框被键入时,我想使用slidedown取消隐藏div。这是代码(部分来自其他来源:jQuery slideDown with forms):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://api.jquery.com/resources/events.js"></script>
<script type="text/javascript">
$("#searched").keypress(function(event) {
if ( event.which == 13 ) {
event.preventDefault();
}
$("#advanced").slideDown();
});
</script>
</head>
<body>
<div id="content">
<form action="mycontrolleraddress" method="post" accept-charset="utf-8" name="search_form" id="search_form">
<input type="text" name="query" value="" placeholder="Search..." id="searched"/><br/>
<div id="advanced" style="display: none;">
From: <input type="text" name="date_from" value="" id="datepicker" onchange="validateDate();" class="datefield" /><br/>
To: <input type="text" name="date_to" value="" id="datepicker2" onchange="validateDate();" class="datefield" /> <br/>
</div>
<input type="submit" name="submit" value="Submit" />
</div>
</body>
</html>
答案 0 :(得分:1)
将您的代码包含在$(document).ready(function(){});
答案 1 :(得分:0)
在$(function() {});
或$(document).ready(function() {});
<script type="text/javascript">
$(function() {
$("#searched").keypress(function(event) {
if ( event.which == 13 ) {
event.preventDefault();
}
$("#advanced").slideDown();
});
});
</script>
答案 2 :(得分:0)
$('#search_form').on('focus', '#searched', function(e){
$('#advanced').slideDown();
})
答案 3 :(得分:0)
解决方案是将你的javascript代码包装在其中:
$(document).ready(function(){
// your code
});