使用者在 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 個瀏覽量

開啟您的職業生涯

完成課程,獲得認證

立刻開始
廣告