javascript以输入形式大写字母

问题描述 投票:0回答:1

嘿伙计们我在这里做错了什么?如果之前已经发布过,我很抱歉,但是我找不到表单输入的好例子。

谢谢。

我真的不明白为什么output.value.toUpperCase()不起作用,或者toUpperCase(output.value)不起作用。

    <html>
      <head>
        <link href="https://fonts.googleapis.com/css?family=Barlow" rel="stylesheet">
      </head>
      <body>

    <h1 id="title">Capitalize a String</h1>
        <form>
          <input type="text" id="entry" placeholder="Enter a string to be capitalized">
        </form>

        <h1 id="title">Output</h1>

        <form>
          <input type="text" id="output" placeholder="Output">
        </form>

        <div id="goBtn">
          <h1 id="goBtnText">
            GO
          </h1>
        </div>
      </body>
    </html>



var goBtn = document.getElementById('goBtn');
var entry = document.getElementById('entry');
var output = document.getElementById('output');

goBtn.addEventListener('click', capitalizeStr);

function capitalizeStr () {
   output.value = entry.value;

  return output.value.toUpperCase();
}
javascript capitalize
1个回答
1
投票

你需要做

function capitalizeStr () {
   output.value = entry.value.toUpperCase();
}

调用output.value.toUpperCase()不会更改output.value属性,它只返回一个新字符串(忽略事件监听器的值qazxs)。

© www.soinside.com 2019 - 2024. All rights reserved.