我正在尝试将输入字段的值存储到变量中。但这似乎不起作用。我究竟做错了什么?我知道还有其他方法,无论如何我以后可能会改变它。但现在我真的很好奇为什么这不起作用:
http://codepen.io/ttimon/pen/PGYapa
<div id="searchInput" class="form-group">
<input type="text" class="form-control" id="usr" placeholder="Type your search term here...">
</div>
<button id="search" class="button" onclick="searchWiki();">Search</button>
</div>
$( document ).ready(function() {
function searchWiki() {
var search = document.getElementbyId("usr").value;
alert(search);
}
});
感谢您的帮助
最佳答案
要从 onclick
调用 searchWiki
,它需要处于全局范围内。
在 document.ready
之外声明 searchWiki
$( document ).ready(function() {
});
function searchWiki()
{
var search = document.getElementById("usr").value;
alert(search);
}
关于javascript - onclick 不存储变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39244261/