如何使用 JavaScript 顯示提示對話方塊?
當你想彈出文字框獲取使用者輸入時,提示對話方塊非常有用。因此,它能使你與使用者進行互動。使用者需要填寫欄位,然後單擊“確定”。
該對話方塊使用稱為 prompt() 的方法顯示,此方法接受兩個引數:(i) 想要在文字框中顯示的標籤,以及 (ii) 想要在文字框中顯示的預設字串。
該對話方塊有兩個按鈕:“確定”和“取消”。如果使用者單擊“確定”按鈕,視窗方法 prompt() 將在文字框中返回輸入的值。如果使用者單擊“取消”按鈕,視窗方法 prompt() 將返回 null。
示例
你可以嘗試執行以下程式碼來了解如何使用 JavaScript 實現提示對話方塊
<html> <head> <script> <!-- function getValue(){ var retVal = prompt("Enter your name : ", "your name here"); document.write("You have entered : " + retVal); } //--> </script> </head> <body> <p>Click the following button to see the result: </p> <form> <input type="button" value="Click Me" onclick="getValue();" /> </form> </body> </html>
廣告