输入的输入值以undefined
的形式返回。我已经看到过这样的其他问题,但没有任何效果。无论如何,当我使用我的jQuery时,它总是给我undefined
。
jQuery的:
$( document ).ready(function() {
$('#Input').keypress(
function(e){
if (e.keyCode == 13) {
alert( ">" + $("#Input").value );
}
});
});
<!DOCTYPE html>
<html>
<style type="text/css">
body{
background-color: black;
color: #00c600;
font-family: VT323;
}
input{
background-color: black;
color: #00c600;
font-family: VT323;
border:none;
width: 98%%;
height: 30px;
font-size: 20px;
border: 2px black;
float: left;
margin-left: 10px;
list-style-type: none;
position: absolute;
bottom: 17px;
}
input:focus {
outline: 0;
}
h1{
text-align: center;
font-size: 50px;
}
h2{
padding-left: 20px;
}
::-webkit-input-placeholder {
color: #00c600;
}
::-webkit-scrollbar {
display: none;
}
</style>
<body>
<script src="http://code.jquery.com/jquery-1.3.2.min.js">
</script>
<script src="jquery.js">
</script>
<link href='http://fonts.googleapis.com/css?family=VT323' rel='stylesheet' type='text/css'>
<h1>FUN TIME BETA 0.1 </h1>
<div id="Text" style="overflow:auto; width:100%; height:400px;">
<h2>>Welcome to Fun Time</h2>
<h2>>Press F11 to enter full screen</h2>
</div>
<h3 style="float:left;list-style-type: none; position: absolute; bottom: 5px;">></h3>
<input name="Input" id="Input" type="text" autofocus> </input>
</body>
</html>
当我发出警报时,undefined
。控制台日志,undefined
。追加,返回错误(错误无关紧要)。我尝试多次更改代码。
答案 0 :(得分:5)
value
是本机DOM元素的属性,$('#Input')
是jQuery对象,它们不具有value
属性,因此出错。
由于您在事件处理程序中执行代码,因此可以使用this.value
或$(this).val()
。
答案 1 :(得分:2)
您需要从
更新代码$("#Input").value
到
$("#Input").val()