简体   繁体   English

Javascript:将用户输入分配为变量

[英]Javascript: Assigning User Input as a Variable

I'm just learning HTML, CSS, and Javascript and having a tough time grasping the JS.我只是在学习 HTML、CSS 和 Javascript,并且很难掌握 JS。 I've been working on this simple issue now for literally 6 hours.我已经在这个简单的问题上工作了 6 个小时。 I've checked plenty of posts on SO, W3, re-read relevant chapters in Jon Duckett's book, and tried every combination I could think of in the code.我查看了很多关于 SO、W3 的帖子,重新阅读了 Jon Duckett 书中的相关章节,并尝试了代码中我能想到的所有组合。

What I need to do is take a username from an input field and run it through a function to change it in some way.我需要做的是从输入字段中获取用户名并通过函数运行它以某种方式更改它。 I've seen other's posts with similar questions but haven't been able to create a solution yet.我看过其他人的帖子有类似的问题,但还没有能够创建一个解决方案。 The issue that keeps happening is that when I place what should be the user input back to the page it uses the actual words name.toUpperCase() rather than the value I've given the variable name .不断发生的问题是,当我将应该是用户输入的内容放回页面时,它使用实际单词name.toUpperCase()而不是我给变量name的值。 I tried converting the name to uppercase in its own statement and then assigning that a variable, but that didn't work either.我尝试在自己的语句中将名称转换为大写,然后为其分配一个变量,但这也不起作用。 It seems like my js is never giving "name" a value to begin with.似乎我的 js 从来没有给“名称”一个值。

JS: JS:

var name = document.getElementById('nameEntry');
var compliment = document.getElementById('compliment');
var input = document.getElementById('submitjs');

input.onclick = transformName;

function transformName () {
    var elcompliment = document.getElementById('compliment');
    elcompliment.textContent = 'name.toUpperCase( )' + ' YOU ROCK!';
};

HTML: HTML:

<div class="jsform">
    <h2> Enter your name and see it change!</h2>
    <input type="text" name="nameEntry" id="nameEntry" />
    <input type="submit" name="submitjs" value="submit" id="submitjs"/>

   <div>
    <p id="compliment">Great Job!</p>
   </div>

</div>

Since you are retrieving input value using element's id, there is no need for form.由于您使用元素的 id 检索输入值,因此不需要表单。

 function transformName () { var name = document.getElementById('nameEntry').value; var compliment = document.getElementById('compliment'); compliment.innerHTML = name.toUpperCase( ) + ' YOU ROCK!'; };
 <div class="jsform"> <h2> Enter your name and see it change!</h2> <input type="text" name="nameEntry" id="nameEntry" /> <input type="button" onclick="transformName()" value="Transform me"/> <div> <p id="compliment">Great Job!</p> </div> </div>

The name variable currently refers to DOM element. name变量当前是指 DOM 元素。 The value property returns the data entered in the input element. value属性返回input元素中input的数据。

The value property sets or returns the value of the value attribute of a text field. value 属性设置或返回文本字段的 value 属性的值。

 var name = document.getElementById('nameEntry').value; var compliment = document.getElementById('compliment'); var input = document.getElementById('submitjs'); input.onclick = transformName; function transformName () { var name = document.getElementById('nameEntry').value; var elcompliment = document.getElementById('compliment'); elcompliment.innerHTML = name.toUpperCase()+' YOU ROCK!'; };
 <div class="jsform"> <h2> Enter your name and see it change!</h2> <input type="text" name="nameEntry" id="nameEntry" /> <input type="submit" name="submitjs" value="submit" id="submitjs" /> <div> <p id="compliment">Great Job!</p> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM