簡體   English   中英

jQuery無法獲取元素上的輸入值

[英]JQuery can't get input value on element

我要做的就是獲取文本輸入的val() 這是一個例子

我不知道是否允許外部鏈接,所以我也將其粘貼在這里:

<div class="input-group">
  <input type="text" id="#test" class="form-control" aria-label="..." style='border-radius:4px;'>
</div>

<script>
  // Always shows up as undefined..not "".
  console.log($("#test").val());
</script>

有誰知道為什么我不能獲得這個元素的價值?

您必須從id屬性中刪除#號:

<input type="text" id="#test" class="form-control" aria-label="..."  ..
//_____________________^

希望這可以幫助。


 <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-3.0.0.js"></script> <script src="https://code.jquery.com/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>Why can't I get the input value of the #test?</title> </head> <body style='padding:64px;'> <div class="input-group"> <input type="text" value="test value" id="test" class="form-control" aria-label="..." style='border-radius:4px;'> </div> <script> // Always shows up as undefined..not "". console.log($("#test").val()); </script> </body> </html> 

暫無
暫無

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

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