简体   繁体   English

如何将用户输入保存到JavaScript中的变量中?

[英]How to save user input into variable in JavaScript?

Given the following HTML: 鉴于以下HTML:

<div id= "playerNames" class="playerInfo">
            <form name="form1" method="post" action="http://examples.funwebdev.com/process.php" id="newGame">
                <p id= "userInput">
                    <label>Player 1 name: </label></br><input type="text" name="p1" id="nameOne" value="" required></input></br>
                    <label>Player 2 name: </label></br><input type="text" name="p2" id="nameTwo" required></input> <input id="submit" type="submit" value="New Game"/>
                </p>
            </form>
        </div>

And the following javascript: 和以下javascript:

 $(document).ready(function () {
    console.log("jQuery is ready to use...");
    $("#newGame").on("submit", newGameListener);
});

function setUpUsers(){
    var player_One = document.getElementById("nameOne").value;
    var player_Two = document.getElementById("nameTwo").value;
    document.getElementById("pTurns").innerHTML = (+nameTwo+ ", its your turn");
    document.getElementById("pScores").innerHTML = (+nameOne+ ": 50pts </br>" (+nameTwo+ ": 50pts </br>"
}

function newGameListener(e) {
    e.preventDefault();
    setUpUsers();
}

I am trying to append these two variables (nameOne, nameTwo) within p elements. 我正在尝试在p元素内附加这两个变量(nameOne,nameTwo)。 However when this code is run I get NaN (Not a Number) Instead of the user input as a string. 但是,当运行此代码时,我得到的是NaN(不是数字),而不是用户输入的字符串。 Not sure how I should go about achieving this! 不知道我应该如何实现这一目标!

That's because you are converting a DOM element to a number. 那是因为您要将DOM元素转换为数字。 The result will be, em.., NaN . 结果将是,例如NaN What you want is the value of the element. 您想要的是元素的值。 You can get the value by reading the .value property. 您可以通过读取.value属性来获取值。

document.getElementById("pTurns").innerHTML = (+nameTwo.value + ", its your turn");

However, since you want to concatenate the value with a string there is no need to use + operator. 但是,由于要使用字符串连接值,因此无需使用+运算符。

Also note that should not rely on the global variables (some browsers like Chrome creates global variables that refer to elements that have the corresponding IDs) for accessing the elements, it's fragile and may fail miserably. 还要注意,访问元素时不应依赖全局变量(某些浏览器(例如Chrome浏览器会创建引用具有相应ID的元素的全局变量)),它很脆弱,可能会失败。 Yes, it has been standardized by HTML5 , but in my opinion it's a bad practice. 是的, 它已经通过HTML5进行了标准化 ,但是我认为这是一个不好的做法。 Use the document.getElementById method for selecting the elements by their id. 使用document.getElementById方法通过元素ID选择元素。

As @Vohuman and @guest271314 answered about your problem( NaN ) but you have several syntax error in your HTML and javascript, however I fixed that I optimized your javascript. 由于@Vohuman和@ guest271314回答了有关您的问题的内容( NaN ),但您的HTML和javascript中有一些语法错误,但是我已修复了我优化了javascript的问题。

Jsfiddle 的jsfiddle

 $(document).ready(function() { console.log("jQuery is ready to use..."); $("#newGame").submit(function(e) { e.preventDefault(); var player_One = $('#nameOne').val(); var player_Two = $('#nameTwo').val(); $('#pTurns').html(player_One + ", its your turn"); $('#pScores').html(player_One + ": 50pts </br>" + player_Two + ": 50pts </br>"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="playerNames" class="playerInfo"> <form name="form1" method="post" id="newGame"> <p id="userInput"> <label>Player 1 name:</label> <br /> <input type="text" name="p1" id="nameOne" value="" required /> <br /> <label>Player 2 name:</label> <br /> <input type="text" name="p2" id="nameTwo" required /> <input id="submit" type="submit" value="New Game" /> </p> </form> </div> <p id="pTurns"></p> <p id="pScores"></p> 

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

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