2016-06-26 3 views
1

Все, что я пытаюсь сделать, это получить val() текстового ввода. Вот example.JQuery не может получить входное значение для элемента

Я не знаю, если внешние ссылки разрешены, поэтому я также вставить здесь:

<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> 

Кто-нибудь знает, почему я не могу получить значение этого элемента?

ответ

2

Вы должны удалить # знак из атрибута 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>

+0

Ах .. :(это то, что происходит, когда вы остаетесь слишком долго. Спасибо хоть за какое-то время там я думал, что это было что-то делать с теневым элементом под вход. – ThatGuy343

Смежные вопросы