简体   繁体   English

无需登录即可记住用户的表单输入

[英]Remembering user's form input without logging in

I've created a simple site where visitors can vote on a particular subject. 我创建了一个简单的网站,访客可以在该网站上对特定主题进行投票。 It consists of a single question and three choices: 它由一个问题和三个选择组成:

<template name="addVote">
    <div class="form-row choices">
        <label><input type="radio" name="vote" value="Yes"><i></i><span>Yes</span></label>
        <label><input type="radio" name="vote" value="No"><i></i><span>No</span></label>
        <label><input type="radio" name="vote" value="Undecided"><i></i><span>I’m Undecided</span></label>
    </div>
    <div class="form-row">
        <button class="button-submit">Submit and view results</button>
    </div>
</template>

I'm using the Session Amplify package to store form values in localStorage as well as the DB, which is working perfectly. 我正在使用Session Amplify程序包将表单值存储在localStorage和数据库中,这非常正常。

var vote = $('[name="vote"]:checked').val();
Meteor.call("addVote", vote, function(error, voteId) {
    SessionAmplify.set('vote', vote);
});

After the visitor has logged their vote they're shown the global results of the vote as a bar cart. 访客记录他们的投票后,他们会以条形图的方式显示投票的全局结果。 They can only see the results after they vote, though. 不过,他们只能在投票后才能看到结果。

What I need to do is remember the user's vote when they visit the site again so they can bypass the vote and go straight to the results, all without forcing them to create an account or log in. I'm already storing their response in localStorage that are accessible by simply running SessionAmplify.get('vote') . 我需要做的是记住用户再次访问该站点时的投票,以便他们可以绕过投票直接进入结果,而无需强迫他们创建帐户或登录。我已经将他们的响应存储在localStorage中只需运行SessionAmplify.get('vote')

How can I use these stored values to pre-populate the form and show them the results? 如何使用这些存储的值预填充表单并显示结果?

    if (localStorage.vote) {
        //user has already cast a vote, reflect this
        document.querySelector(".form-row").querySelector("input[value='"+localStorage.vote+"']").setAttribute("checked", true);
        //disable the submit
        document.querySelector(".button-submit").setAttribute("disabled", true);
    } 

    function setLocalStorageVote()
    {
         //call when user has cast the vote.
         localStorage.vote = document.querySelector(".form-row").querySelector("input[type='radio']:checked").value;
    }


 document.querySelector(".button-submit").onclick = setLocalStorageVote; //using onclick here just for demonstrating purposes. Use addEventListener in production code!
<template name="addVote">
    <div class="form-row choices">
        <label><input type="radio" name="vote" value="Yes"><i></i><span>Yes</span></label>
        <label><input type="radio" name="vote" value="No"><i></i><span>No</span></label>
        <label><input type="radio" name="vote" value="Undecided"><i></i><span>I’m Undecided</span></label>
    </div>
    <div class="form-row">
        <button class="button-submit">Submit and view results</button>
    </div>
</template>

Should do it. 应该做。 Just store a Boolean and bypass the code to get to the results based on the value of localStorage.vote. 只需存储一个布尔值并绕过代码即可根据localStorage.vote的值获得结果。 localStorage is set indefinitely on the client where the vote was cast. localStorage投票的客户端上无限期设置localStorage Please note that I've removed the Meteor code since I cannot use this in a fiddle . 请注意,由于我无法在小提琴中使用它,因此我删除了Meteor代码。 This is just to demonstrate that localStorage works. 这只是为了证明localStorage可以工作。

http://jsfiddle.net/8fzur0h9/1/ http://jsfiddle.net/8fzur0h9/1/

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

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