簡體   English   中英

我想要輸入的值,但它總是警報0

[英]i want the value of the input but it always alert 0

我想要輸入的值,但它總是警告0,如何獲取輸入的值

在下面,我無法獲取值-輸入的值只是數字

 $(document).ready(function() { var text = Number($("#text").val()) $("#btn").click(function() { $("#p").text(text) }) }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="text" placeholder="Enter a number" class="txt"><br> <button id="btn">RUN</button> <p id="p"></p> 

問題在於頁面加載時會獲得變量值,而以后再也不會獲得它。 每當您單擊按鈕時,都需要獲取它。 我已經為您編輯了它,現在可以使用了。

 $(document).ready(function(){
     $("#btn").click(function(){
          var text = Number($("#text").val())
          $("#p").text(text)
      })
  })

您是否在頁面上添加了js庫?

  <!DOCTYPE html> <html> <body> <head> <script src="//code.jquery.com/jquery-3.3.1.js"></script> </head> <input type="text" id="text" placeholder="Enter a number" class="txt"><br> <button id="btn">RUN</button> <script> $(document).ready(function() { $("#btn").click(function() { alert($("#text").val()); }) }); </script> </body> </html> 

暫無
暫無

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

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