Javascript和HTML
$( document ).ready(function() {
var inputMessage = $("#inputMessage");
var sendButton = $("#sendButton");
console.log(inputMessage.value); //->undefined
inputMessage.value = "hello";
console.log(inputMessage.value); //->"hello"

<!DOCTYPE html>
<html lang="es">
<head>
<title>Chat de Hernan</title>
<!--jquery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<!--bootstrap-->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
</head>
<body>
<div class="input-group">
<input id="inputMessage" class="form-control" type="text" value="goodbye" placeholder="Escriba aqui">
<span class="input-group-btn">
<button id="sendButton" class="btn btn-default">Enviar</button>
</span>
</div>
<!--js-->
<script type="text/javascript" src="script.js"></script>
</body>
</html>
&#13;
我遇到的问题是未定义和&#34; hello&#34; 的值与再见>的值不同
<input id="inputMessage" class="form-control" type="text" value="goodbye" placeholder="Escriba aqui">
这怎么可能? value =&#34; goodbye&#34; 语句指出输入有一个&#34;再见&#34;值,但javascript表示它的值未定义,并且&#34; hello&#34;,respectibely。
答案 0 :(得分:2)
答案 1 :(得分:0)
定义var inputMessage = $("#inputMessage");
使inputMessage
jQuery元素具有方法val()
来获取/设置元素值。
因此,请使用inputMessage.val()
或inputMessage.val('hello')
来设置新值,而不是'inputMessage.value'。
答案 2 :(得分:0)
这是因为您将输入作为jQuery对象获取,但您正在使用JavaScript函数。相反,请将value
更改为val()
。另外,请勿忘记关闭.ready
。
$( document ).ready(function() {
var inputMessage = $("#inputMessage");
var sendButton = $("#sendButton");
console.log(inputMessage.val()); //->undefined
inputMessage.val("hello");
console.log(inputMessage.val()); //->"hello"
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="es">
<head>
<title>Chat de Hernan</title>
<!--jquery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<!--bootstrap-->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
</head>
<body>
<div class="input-group">
<input id="inputMessage" class="form-control" type="text" value="goodbye" placeholder="Escriba aqui">
<span class="input-group-btn">
<button id="sendButton" class="btn btn-default">Enviar</button>
</span>
</div>
<!--js-->
<script type="text/javascript" src="script.js"></script>
</body>
</html>
&#13;