當用戶在 HTML 中的搜尋欄位中輸入內容後執行指令碼?


在 HTML 中使用 onsearch 屬性,以便當使用者在搜尋欄位中輸入內容並按 ENTER 或 x 時執行指令碼。

例項

你可以嘗試執行以下程式碼以實現 onsearch 屬性:

<!DOCTYPE html>
<html>
   <body>
      <p>Search something and press ENTER.</p>
      <input type = "search" id="inputID" onsearch = "display()">
      <p><strong>Note:</strong> It won' work in Internet Explorer and Firefox.</p>
      <p id="test"></p>
      <script>
         function display() {
         var a = document.getElementById("inputID");
         document.getElementById("test").innerHTML = "Searched for - " + a.value;
         }
      </script>
   </body>
</html>

更新於: 03-Mar-2020

151 瀏覽

開啟你的職業生涯

完成課程認證

立即開始
廣告