我想做以下事情:
- 创建 3 个文本框和 1 个按钮
- 在 3 个框中输入小写字母
- 点击按钮
- 点击按钮后,字母将转换为大写
为什么我在框中输入的字母没有转换为大写?
这是我的代码:
<html>
<head>
<script>
function upperCase() {
var x = document.getElementById("form_1");
x = x.value.toUpperCase();
}
</script>
</head>
<body>
<form name="form1" id="form_1" method="post">
Enter your name:
<input type="text" name="nameInput">
Enter your street address:
<input type="text" name="streetInput">
Enter your city/state:
<input type="text" name="cityStateInput">
<input type="button" name="upperCaseButton" value="Convert to Uppercase"
onclick="upperCase()">
</form>
</body>
</html>
最佳答案
您需要重新分配每个个体的值<input>
元素为其大写版本。在下面的脚本中,我添加了一个辅助方法,它接受元素 id 并尝试将该元素的值更改为大写。 upperCase()
函数仍然是入口点,因此您根本不需要修改 HTML 代码。
<script>
function upperCaseInput(var id) {
var x = document.getElementById(id);
x.value = x.value.toUpperCase();
}
function upperCase() {
upperCaseInput("nameInput");
upperCaseInput("streetInput");
upperCaseInput("cityStateInput");
}
</script>
用法:
<input type="button" name="upperCaseButton" value="Convert to Uppercase"
onclick="upperCase()">
关于javascript - 同时将多个文本输入转换为大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35495334/