Javascript将小写转换为大写

时间:2011-08-10 14:05:29

标签: javascript html

以下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>

con.js

function convert() 
{
    var str ;
    str = document.getElementById("field1");
    document.writeln(str.toUpperCase());
}

为什么上面的代码没有给我想要的结果?

3 个答案:

答案 0 :(得分:7)

您需要将其更改为:

var str = document.getElementById("field1").value;
document.writeIn(str.toUpperCase());

答案 1 :(得分:7)

尝试:

str = document.getElementById("field1").value;

这是因为getElementById返回对HTML元素的引用,而不是包含的“text”值。

答案 2 :(得分:1)

以下更改应解决您的问题:

str = document.getElementById("field1");

应该是

str = document.getElementById("field1").value;
相关问题