2013-02-24 75 views
0

我不知道爲什麼這不起作用?我嘗試了幾種方法並環顧四周,但沒有一個例子適用於我。我的代碼:提交/轉到專注於輸入並按下輸入時

HTML

<div id='SearchBar'> 

    <form id='SearchBar'> 

     <input placeholder='Search' id='SearchBar'></input> 

    </form> 

</div> 

CSS

#SearchBar{ 
       background:#ffffff; 
       width:224px; 
       height:32px; 
       float:right; 
      } 

       form#SearchBar { 
        width:100%; 
        height:100%; 
       } 

        input#SearchBar { 
         font-size:16px; 
         border:none; 
         width:208px; 
         height:32px; 
         padding:0 8px; 
        } 

         input#SearchBar:focus{ 
          outline-color:#2c5aa0; 
         } 

jQuery的

$('input#SearchBar').keypress(function(event) { 

    if (event.which == 13) { 

       window.location = list.html; //or submit or do whatever 

    } 

}); 

任何幫助表示讚賞。我哪裏錯了?

回答

0

keyup事件,你缺少引號爲list.htmllist對象是undefined因此它不具有html財產!

$(function(){ 
    $('#SearchBar').keyup(function(event) { 
     if (event.which == 13) { 
      window.location.href = 'list.html'; 
     } 
    }); 
}) 
+0

我不知道爲什麼,但仍然不適合我...讓我加我的HTML,也許這將有所幫助? – jstacks 2013-02-24 04:44:16

+0

@jstacks ID必須是唯一的,jQuery只會選擇具有該特定ID的第一個元素。 – undefined 2013-02-24 08:46:33