2013-04-22 55 views
0

我有一個具有高級選項的表單。高級選項在div(隱藏)中。當「搜索」輸入框被鍵入時,我想使用滑動來取消隱藏div。下面的代碼(從其他來源部分採取:jQuery slideDown with forms):KeyPress上的JQuery SlideDown

<!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> 

回答

1

包裹內$(document).ready(function(){});

+0

@KenJ很高興它幫助,請標記答案爲接受,如果它解決了你的問題:) – 2013-04-22 18:43:35

0

使用內部$(function() {});$(document).ready(function() {});

<script type="text/javascript"> 
    $(function() { 
$("#searched").keypress(function(event) { 
    if (event.which == 13) { 
    event.preventDefault(); 
    } 
     $("#advanced").slideDown(); 

}); 
}); 
    </script> 
0
$('#search_form').on('focus', '#searched', function(e){ 
    $('#advanced').slideDown(); 
}) 
0

解決辦法是換你的JavaScript代碼裏面的代碼:

$(document).ready(function(){ 
    // your code 
});