简体   繁体   English

jQuery-确保用户不会连续两次在文本输入中提交相同的值

[英]jQuery - Making sure user doesn't submit same value in text input twice in a row

I'm creating a magic 8 ball app where the the user enters a question in an input box. 我正在创建一个魔术8球应用程序,用户可以在其中在输入框中输入问题。 They press the submit button and the answer replaces the default html text in a heading. 他们按下提交按钮,答案将替换标题中的默认html文本。 If the same question is asked twice in a row I want to display an alert message instead of giving a new answer. 如果连续两次问相同的问题,我想显示一条警报消息,而不是给出新的答案。 I'm new to jQuery and I can't quite figure out how to store the last value entered. 我是jQuery的新手,我不太清楚如何存储最后输入的值。 I would really appreciate some input. 我非常感谢您的投入。 Thank you in advance. 先感谢您。

$(document).ready(function(){
    var questionValue;
    $("#submit").click(function() {
        //Make sure user doesn't ask same question twice in a row
        if (questionValue != $("#submit").val()) {

            //List of possible responses
            var response = ["Ask again later…", "Yes", "No", "It appears so", "Reply is hazy, please try again", "Yes, definitely", "What is it you really want to know?", "Outlook is good", "My sources say no", "Signs point to yes", "Don't count on it", "Cannot predict now", "As I see it, yes", "Better not tell you now", "Concentrate and ask again"];

            //generate a random number between 0-14
            var randomNumber = Math.floor(Math.random() * 15);

            //Present random answer
            $("#eightBallAnswer").text(response[randomNumber]);
        }
        else {
            alert("Ask a different question");
        }
        var questionValue = $("#submit").val();
    });
});

Remove the var from the last line of your click handler function, because that is creating a local variable inside that function, and the value isn't retained between calls to the click handler. 从单击处理程序函数的最后一行删除var ,因为这是在该函数内部创建一个局部变量,并且在两次单击处理程序的调用之间均未保留该值。 You want to be referring to the questionValue variable declared outside the click handler. 您要引用在单击处理程序外部声明的questionValue变量。

So change: 所以改变:

var questionValue = $("#submit").val();

...to: ...至:

questionValue = $("#submit").val();

EDIT: I just noticed that you are getting the value of an element with the id submit , the same element the click handler is bound to, which doesn't seem right. 编辑:我刚刚注意到您正在获取ID为submit的元素的值,该元素与单击处理程序绑定到的元素相同,这似乎不正确。 The button's value won't change between clicks. 在两次点击之间,按钮的值不会改变。 You don't show the HTML, so I'm not sure what the ID of the text box is, but you should double-check that selector in the if condition and on the line I've shown. 您没有显示HTML,所以我不确定文本框的ID是什么,但是您应该在if条件和显示的行中再次检查该选择器。

Im finding it hard to explain my changes haha 我发现很难解释我的变化哈哈

   $(document).ready(function(){
        var prevquestionValue=$("#input").val();
        $("#submit").click(function() {
            //Make sure user doesn't ask same question twice in a row
            if (prevquestionValue!= $("#input").val()) {

                //List of possible responses
                var response = ["Ask again later…", "Yes", "No", "It appears so", "Reply is hazy, please try again", "Yes, definitely", "What is it you really want to know?", "Outlook is good", "My sources say no", "Signs point to yes", "Don't count on it", "Cannot predict now", "As I see it, yes", "Better not tell you now", "Concentrate and ask again"];

                //generate a random number between 0-14
                var randomNumber = Math.floor(Math.random() * 15);

                //Present random answer
                $("#eightBallAnswer").text(response[randomNumber]);
            }
            else {
                alert("Ask a different question");
            }
            prevquestionValue = $("#input").val();
        });
    });

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

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