在JavaScript中,可以通过多种方式存储从文本框(input element)输入后的字符串值。以下是一些常见的方法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Store Input Value</title>
</head>
<body>
<input type="text" id="myInput">
<button onclick="storeValue()">Store Value</button>
<script>
function storeValue() {
var inputValue = document.getElementById('myInput').value;
console.log(inputValue); // 这里可以对inputValue进行进一步处理
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Store Input Value</title>
</head>
<body>
<input type="text" id="myInput">
<button onclick="storeValue()">Store Value</button>
<script>
var globalInputValue;
function storeValue() {
globalInputValue = document.getElementById('myInput').value;
console.log(globalInputValue);
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Store Input Value</title>
</head>
<body>
<input type="text" id="myInput">
<button onclick="storeValue()">Store Value</button>
<script>
function storeValue() {
var inputValue = document.getElementById('myInput').value;
localStorage.setItem('userInput', inputValue);
console.log(localStorage.getItem('userInput'));
}
</script>
</body>
</html>
getElementById
中的ID与HTML元素的ID匹配,且元素已正确加载。通过上述方法,可以有效地在JavaScript中存储和处理文本框输入的值。
领取专属 10元无门槛券
手把手带您无忧上云