最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript to convert lowercase to upper case - Stack Overflow

programmeradmin3浏览0评论

What is wrong with the following htmla and javascript code

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());
}

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

What is wrong with the following htmla and javascript code

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());
}

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

Share Improve this question edited Oct 27, 2017 at 19:35 Luke T Brooks 5571 gold badge8 silver badges27 bronze badges asked Aug 10, 2011 at 14:05 stationstation 7,14516 gold badges59 silver badges93 bronze badges 3
  • 1 What result does it give you? – rlemon Commented Aug 10, 2011 at 14:07
  • People gets reputation to downvote innocent question, why buddies? – SMI Commented Feb 4, 2014 at 12:29
  • @SMI: I didn't down vote this question, but the one person who did probably did so because the OP didn't specify the difference between the expected result and the actual result. In other words, they didn't identify the unwanted symptom(s). – Jeromy French Commented Apr 21, 2015 at 16:46
Add a comment  | 

3 Answers 3

Reset to default 7

Try:

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

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

You need to change it to this:

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

The following change should fix your issue:

str = document.getElementById("field1");

should be

str = document.getElementById("field1").value;
发布评论

评论列表(0)

  1. 暂无评论