简体   繁体   English

如何将来自HTML文本字段的输入永久存储到jQuery变量中?

[英]How do I permanently store input from an HTML text field into a jQuery variable?

jQuery jQuery的

$(document).ready(function(){ 
    var guess;
    $('#submit').on("click", function() {
        guess = $('#guess-value').val();
        $("#value").text(guess);
        alert(guess);
    });
    alert(guess);
});

HTML HTML

<div id='game'>
    <form id='user-input'>
        <input type='text' id='guess-value' placeholder='1-100'></input>    
        <button id='submit'>Submit</button>
    </form>

    <h4 id='guess-count'>Attempts left: <span id="attempts">6</span></h4>       
</div>

<h4 id='checker'>The value entered is <span id="value">?</span></h4>

I've provided snippets of my HTML and jQuery code above. 我在上面提供了我的HTML和jQuery代码的摘要。 I am trying to store a number that has been entered into a text field, into a jQuery variable called guess after pressing a submit button. 我试图在按下提交按钮后将已输入文本字段中的数字存储到名为guess的jQuery变量中。

The following happens occurs: When I enter a number into the field and press submit, I get an alert showing the value I entered. 发生以下情况:当我在该字段中输入数字并按Submit时,我会收到一条警报,显示输入的值。 After closing the event I get another alert that is supposed to show the value of 'guess' and the value is undefined. 关闭事件后,我会收到另一个警报,该警报应该显示“ guess”的值,并且该值未定义。

This happens even though I declared the variable guess outside of the click event. 即使我在click事件之外声明了变量guess也会发生这种情况。 Why is this and how do I permanently store the value? 为什么会这样,我如何永久存储值?

You are using a <form> element to ask for user input. 您正在使用<form>元素要求用户输入。 The problem with a form, is that when it submits, it wants to navigate away from (or refresh) the page. 表单的问题在于,提交表单时,它想离开页面(或刷新页面)。 When the page refreshes, all js is lost. 页面刷新时,所有js都将丢失。

Simple fix: don't use a form (you can use a DIV instead). 简单的解决方法:不要使用表单(您可以使用DIV)。

Alternatively, you can tell the form to NOT do its default action of submitting by using event.preventDefault() : 或者,您可以使用event.preventDefault()告诉表单不执行其默认的提交操作:

jsFiddle Demo jsFiddle演示

HTML: HTML:

<div id='game'>
    <form id='user-input'>
        <input type='text' id='guess-value' placeholder='1-100'></input>
        <button id='submit'>Submit</button>
    </form>
     <h4 id='guess-count'>Attempts left: <span id="attempts">6</span></h4> 
</div>

<h4 id='checker'>The value entered is <span id="value">?</span></h4>

<input type="button" id="myButt" value="Show Value" />

jQuery: jQuery的:

var guess;
$('#submit').on("click", function (evnt) {
    guess = $('#guess-value').val();
    $("#value").text(guess);
    alert(guess);
    evnt.preventDefault();
});

$('#myButt').click(function(){
   alert( guess ); 
});

Further Notes: 附加说明:

Note that the 2nd alert(guess) in your posted code will occur immediately upon document.ready. 请注意,您发布的代码中的第二次alert(guess)将在document.ready时立即发生。 I mean, immediately -- as soon as the DOM is ready. 我的意思是, 立即 -DOM准备就绪。 Before anything has been put into guess . 在没有任何猜测之前 That is why it returns undefined. 这就是为什么它返回undefined的原因。

That is probably not what you want. 那可能不是您想要的。 Code example above adds a button to allow you to view that variable's contents when desired. 上面的代码示例添加了一个按钮,使您可以在需要时查看该变量的内容。

The function : $("#value").text(guess) is not corret in this case, replace it with : 在这种情况下,函数: $("#value").text(guess)不是正确的,请将其替换为:

$("#value").empty().append(guess);

you should wait to be . 你应该等待。 ready() in order to submit(); ready()以便提交();

give me feedback please. 请给我反馈。 enjoy :) 请享用 :)

The 'guess' variable is out of the click event handler but it's in the ready event handler; 'guess'变量不在click事件处理程序中,但在ready事件处理程序中; So the second alert box will be shown exactly once when the page is loaded. 因此,页面加载后,第二个警告框将仅显示一次。 It will then be undefined. 然后它将是不确定的。 The click events will occur later. 点击事件将在以后发生。

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

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