向导 - “重新开始”按钮(Wizard - “Start Over” buttons)

2019-09-26 07:39发布

我创建了我的“精灵”,我有工作,我需要它的方式,但我的问题是如何创建的结果页面,将清除所有单选按钮和显示的起始格一个“重新开始”按钮?

目前,我有2周里面的div的结果相同的按钮,但只有第一个作品。 救命! (选择“问题1 - 答案1”,那么“问题2 - 回答1或2”。得到什么我谈论)

http://jsfiddle.net/dswinson/PXp7c/56/

此外,如果你对我怎样才能实现这个向导,是另一个想法更容易,请让我知道。

Answer 1:

正如你可能从评论看,你的脚本是有点不对 。 这种编程方式是不以任何way..it在一定程度上可能会工作动态,但正如你可能已经经历了可怕的维护。

对于这样的应用,你需要做一个核心插件处理某种数组,包含的问题和答案。 这似乎是一个有趣的挑战,所以我创建了一个插件,是基于你的例子参数。 其所谓的DawnWizard。

我希望,明白这个插件的一般概念,并适用于你的下一个项目同样的方法:)

/*
 * DawnWizard v1.0
 * Originally made for Dawn's question in StackOverflow
 * http://stackoverflow.com/questions/6378814/
 * 
 * Made by Kalle H. Väravas
 * http://stackoverflow.com/users/721553/
 * 
 * No direct copyright or licences.
 * Use however you want, just give me lots of +1 :)
*/
(function ($) {
    $.fn.exists = function () {return $(this).length > 0;}
    $.fn.DawnWizard = function (input_setup) {
        var default_setup = {
            title: 'Demo wizard',
            description: 'Welcome text..'
        };
        setup = jQuery.extend(default_setup, input_setup || {});
        var wizard_container = $(this);
        var questions_count = 0;
        jQuery.each(setup['questions'], function () {
            questions_count++;
        });
        load_startup = function () {
            results = [];
            wizard_container.empty().append(
                $('<h1>').html(setup['wizard']['title']),
                content_container = $('<div id="content">').append(
                    $('<p>').html(setup['wizard']['description']),
                    $('<button id="start_wizard" />').text('Start the Wizard')
                )
            );
            $('#start_wizard').click(function () {
                load_question(current_qid = 1);
            });
        };
        load_results = function () {
            content_container.empty().append(results_container = $('<p>'));
            jQuery.each(setup['questions'], function (i, question) {
                results_container.append($('<div>').text(explain_qid(i) + ' - ' + results[i]));
            });
            content_container.after($('<button id="start_over">').text('Start over'));
            current_qid = 1;
            $('#start_over').click(function () {
                load_startup(current_qid = 1);
            });
        };
        load_question = function (qid) {
            if (qid == 0) {
                load_startup();
                return;
            } else if (qid == questions_count + 1) {
                load_results();
                return;
            }
            content_container.empty().append(
                $('<p>').append(
                    $('<b id="question">').html(setup['questions'][qid]['question']),
                    questions = $('<ul>')
                ),
                $('<button id="previous">').text('Previous'),
                $('<button id="next">').text('Next')
            );
            jQuery.each(setup['questions'][qid]['answers'], function (i, answer) {
                questions.append($('<li><input type="radio" name="answer" value="' + answer + '"> ' + answer + '</li>'));
            });
            $('#previous, #next').click(function () {
                var action =  $(this).attr('id');
                var checked_answer = $('input[name=answer]:checked');
                if (action == 'previous') {
                    load_question(current_qid = current_qid - 1);
                } else if (action == 'next') {
                    if (checked_answer.size() > 0) {
                        insert_result(current_qid, checked_answer.val());
                        load_question(current_qid = current_qid + 1);
                    } else {
                        add_message('You forgot to check an answer!', 'error');
                    }
                }
            });
        };
        insert_result = function (qid, answer) {
            results[qid] = answer;
        };
        explain_qid = function (qid) {
            return setup['questions'][qid]['question'];
        };
        add_message = function (message, type) {
            if (jQuery.inArray(type, ['error', 'success'])) {
                return;
            }
            if (!$('#message').exists()) {
                content_container.before(
                    $('<div id="message" class="' + type + '">')
                    .text(message)
                    .fadeIn()
                    .delay(2000)
                    .fadeOut('normal', function () {
                         $(this).remove();
                     })
                );
            }
        };
        load_startup();
    };
}) (jQuery);

[ 查看输出 - 评论版 ]

如何设置的问题和答案,称这是:

$('#put_wizard_here').DawnWizard({
    wizard: {
        title: 'Demo wizard',
        description: 'Welcome text..'
    },
    questions: {
        1: {
            question: 'Who is hotter?',
            answers: [
                'Miley', 'Selena', 'Mila'
            ]
        },
        2: {
            question: 'Who is the smartest?',
            answers: [
                'Kalle H. Väravas', 'Kalle\'s colleague'
            ]
        },
        3: {
            question: 'Coolest StackExchange?',
            answers: [
                'StackOverflow', 'Programmers', 'Gaming'
            ]
        }
    }
});

[ 查看输出 - 评论版 ]



文章来源: Wizard - “Start Over” buttons
标签: jquery wizard