我想提醒文本框topValue的值,但是当调用solve ()时,会出现一个文本框,但是没有文本/值/数字
这是我的代码:
var topValue = document.getElementById('topValue').value
function solve() {
alert(topValue);
}
$('#solveButton').click(function () {
solve();
});发布于 2016-02-19 03:21:28
textbox的值首先从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)
});
});发布于 2016-02-19 03:36:06
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function () {
var topValue = document.getElementById('topValue').value; // have the initial value
function solve() {
alert(topValue);
alert(document.getElementById('topValue').value) // current value
}
$('#solveButton').click(function () {
solve();
});
});
</script>
</head>
<body style="width:50%;">
<input type="text" id="topValue" value="ssss"/>
<input type="button" value="Solve" id="solveButton" />
</body>
</html>https://stackoverflow.com/questions/35496705
复制相似问题