以下htmla和javascript代码有什么问题
formToConvert.html
<html>
<head>
<title>ExampleToConvert</title>
<script type = "text/javascript" src = "con.js"></script>
</head>
<body>
<form id ="myform">
<input type = "text" id = "field1" value = "Enter text Here"/><br/>
<input type ="submit" value = "submit" onclick = "convert()"/>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
con.js
function convert()
{
var str ;
str = document.getElementById("field1");
document.writeln(str.toUpperCase());
}
Run Code Online (Sandbox Code Playgroud)
为什么上面的代码没有给我想要的结果呢?
您需要将其更改为:
var str = document.getElementById("field1").value;
document.writeIn(str.toUpperCase());
Run Code Online (Sandbox Code Playgroud)
尝试:
str = document.getElementById("field1").value;
Run Code Online (Sandbox Code Playgroud)
这是因为getElementById返回对HTML元素的引用,而不是包含的"text"值.