如何检查本地存储中的密钥是否有值?

时间:2017-04-01 01:10:57

标签: javascript object if-statement local-storage

如果密钥中没有预先存在的值,则用户应该只能存储信息。如何检查是否已存在值,并提醒用户已存在值?我知道我必须使用if语句。如果有值警告用户。否则设置项目...(保存对象)。但我该如何检查价值?

function addEvent() {
    var announcement = {
        title: document.getElementById('title').value,
        group: document.getElementById('group').value,
        author: document.getElementById('author').value,
        type: document.getElementById('type').value,
        date: document.getElementById('date').value,
        time: document.getElementById('time').value,
        gender: document.getElementById('gender').value,
        grade: document.getElementById('grade').value,
        message: document.getElementById('message').value
    };
    var objS = JSON.stringify(announcement);

    localStorage.setItem('announcement', objS);

    alert('Announcement successfully stored!');
}

function showA() {
    var getObj = localStorage.getItem('announcement');
    var objP = JSON.parse(getObj);

    document.getElementById('showAnnouncement').innerHTML = "Announcement"+"<br/>Title:"+objP.title+"<br/>Group:"+objP.group+"<br />Author:"+objP.author+"<br />Type:"+objP.type+"<br/>Date:"+objP.date+"<br />Time:"+objP.time+"<br/>Gender:"+objP.gender+"<br/>Grades:"+objP.grade+"<br />Message:"+objP.message;
}

1 个答案:

答案 0 :(得分:0)

我认为以下代码段可以为您提供帮助!

package AssignmentFace;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


public class AssignmentFace extends JFrame {

    private JPanel contentPane;
    private ImageIcon head;
    private JCheckBox checkBoxBrows;
    private static ImageIcon brows; 

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AssignmentFace frame = new AssignmentFace();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the frame.
     */
    public AssignmentFace() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(1,2));
        setContentPane(contentPane);

        JPanel selectionPanel = new JPanel();
        selectionPanel.setLayout(new GridLayout(7,1));
        contentPane.add(selectionPanel, BorderLayout.WEST);

        // Contains the checkboxes
        checkBoxBrows= new JCheckBox("brows");
        selectionPanel.add(checkBoxBrows);

        JCheckBox checkBoxNose = new JCheckBox("Nose");
        selectionPanel.add(checkBoxNose);

        JCheckBox checkBoxMouth = new JCheckBox("Mouth");
        selectionPanel.add(checkBoxMouth);

        Handler handler = new Handler();

        JButton btnCycle = new JButton("Cycle ...");
        btnCycle.addActionListener(handler);
        selectionPanel.add(btnCycle);

        JPanel facePanel = new JPanel();
        facePanel.setOpaque(true);
        contentPane.add(facePanel, BorderLayout.CENTER);

        //  Head
        head = new ImageIcon(getClass().getResource("/images/head.png"));
        JLabel headLabel = new JLabel(head, JLabel.CENTER);
        headLabel.setLayout(new BorderLayout());
        facePanel.add( headLabel, BorderLayout.CENTER );

        //  Brows
        brows = new ImageIcon();
        JLabel browsLabel = new JLabel(brows, JLabel.CENTER);
        headLabel.add(browsLabel, BorderLayout.CENTER); 

        //  Nose
        //ImageIcon nose = new ImageIcon();
        //JLabel noseLabel = new JLabel(nose, JLabel.CENTER);
        //facePanel.add(noseLabel, BorderLayout.CENTER);

        // Mouth
        //ImageIcon mouth = new ImageIcon();
        //JLabel mouthLabel = new JLabel(nose, JLabel.CENTER);
        //facePanel.add(mouthLabel, BorderLayout.CENTER);
    }

    public class Handler implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e) 
        {
            if(checkBoxBrows.isSelected())
            {
                brows = new ImageIcon(getClass().getResource("/images/coolBrows.png"));
                System.out.println("here"); //For testing purposes. This does come through in the console
            }
        }
    }
}