该文本框的值首先从DOM中获取。但是,当点击按钮时,会使用相同的缓存值。
这可以通过在函数中移动DOM中读取值的语句来解决。
function solve() {
var topValue = document.getElementById('topValue').value
alert(topValue);
}
注意
$('#solveButton').click(function() {
solve();
});
也可以写成
$('#solveButton').click(solve);
但是,有一个更好的办法。
我建议你使用jQuery从文本框中获取值。
// When DOM is completely loaded
$(document).ready(function() {
// On click of the `solveButton`
$('#solveButton').click(function() {
// Get the value of the `#topValue`
var topValue = $('#topValue').val();
// For debugging use `console.log` instead of `alert`
console.log('topValue', topValue)
});
});
正要说一下使用JQuery一样。不妨使用它,如果它在那里! – Ageonix