簡體   English   中英

我如何向用戶提醒文本框輸入

[英]How do i alert the textbox input to a user

如果我有一個按鈕和一個輸入字段。 當單擊按鈕時,我將如何向用戶提醒輸入字段中的任何內容。

請解釋您的代碼。 讓它盡可能簡單。

<input type="text" id="input" />
<button onclick="displayEnteredText()">Display</button>
<script>
  function displayEnteredText() {
    var inputText = document.getElementById("input"); // get the element with id "input" which is the textField
    alert(inputText.value); // show the value of the input in alert message
  }
</script>

一種可能的方法:

<!DOCTYPE html>
    <html>
    <head></head>
    <body>
     <input id="name" value="">
     <input type="button" value="show me the name" onclick="alert(document.getElementById('name').value)">
    </body>
    </html>

另一種可能的方法:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
        window.onload = function () {
            var buttonElement = document.getElementById('button');
            buttonElement.addEventListener('click', function() {
                alert(document.getElementById('name').value);
            });
        }
    </script>

</head>
<body>
 <input id="name" value="">
 <input id="button" type="button" value="show me the name">
</body>
</html>

使用第二種方法,您可以分離職責,一個人可以創建 de html,另一個人可以專注於創建 javascript 代碼。

有幾種方法可以做到這一點,但我認為在當前情況下,有兩個例子就足夠了

<body>
<input type="text" name="basicText" id="alertInput">
<button class="alertButton">Click me!</button>
</body>

<script type="text/javascript">


    $(".alertButton").click(function(){
        var value = $("#alertInput").val();
        alert(value + " was entered");
    });
</script>

為了顯示您在警報中鍵入的內容,您需要引用文本框中的值。 由於 jquery 在帖子中被標記,我用它來獲取文本框中的內容。

你也可以試試這個

HTML

<input type="button" id="btnclick" style="width:100px" value="Click Me" />
<input type="text" id="txtbox">

JS

$("#btnclick").click(function(){
var txtvalue = $("#txtbox").val();
alert("User enter " + txtvalue);
})

小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM