[英]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.