Javascript Javascript将小写字母转换为大写字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7012114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:15:07  来源:igfitidea点击:

Javascript to convert lowercase to upper case

javascripthtml

提问by station

What is wrong with the following htmla and javascript code

以下htmla和javascript代码有什么问题

formToConvert.html

表单转换.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

con.js

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

Why is the above code not giving me the desired result?

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

回答by James Johnson

You need to change it to this:

你需要把它改成这样:

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

回答by GNi33

Try:

尝试:

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

This is because getElementById returns a reference to your HTML Element, not the "text"-value that is contained.

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

回答by Rion Williams

The following change should fix your issue:

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

str = document.getElementById("field1");

should be

应该

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