简体   繁体   English

使用 javascript 创建调查表

[英]creating a survey form using javascript

I have a project where I need to be able to make a survey form.我有一个项目需要能够制作调查表。 I tried to sort of imitate the way google forms work, wherein initially only an input box for the title of the survey appears, along with an "add question" button provided at the bottom of it.我试图模仿谷歌表单的工作方式,其中最初只显示调查标题的输入框,以及底部提供的“添加问题”按钮。

When the "add question" button is clicked, an input box wherein the question needs to be entered will appear.点击“添加问题”按钮后,会出现需要输入问题的输入框。 Then below the input box for the question, three radioboxes will appear labelled as radio box, check box, and text.然后在问题的输入框下方,将出现三个单选框,分别标记为单选框、复选框和文本。 This is for choosing what the choice types for the questions are.这是用于选择问题的选择类型。 Radio box is for multiple choice type questions, check box is for questions with multiple answers, and text is for questions that don't have choices.单选框用于选择类型的问题,复选框用于有多个答案的问题,文本用于没有选择的问题。

Upon clicking either radiobox or checkbox, two input boxes will appear for the choices.单击单选框或复选框后,将出现两个用于选项的输入框。 There will also be a button called "add choices" under the last choice input box if the survey creator wishes to add more choices for the form.如果调查创建者希望为表单添加更多选项,那么在最后一个选项输入框下还会有一个名为“添加选项”的按钮。 If the text option is clicked, however, the input box for the choices should not appear (or if in the case that radiobox or checkbox were initially chosen, the input boxes for the choices should disappear).但是,如果单击文本选项,则不应出现选项的输入框(或者,如果最初选择了单选框或复选框,则选项的输入框应消失)。

My problem though is that if I click radiobox/checkbox option for the first few questions and then choose text for the next question, the input boxes for choices still appear for the text box when it's not supposed to.我的问题是,如果我单击前几个问题的单选框/复选框选项,然后为下一个问题选择文本,则在不应该出现时,文本框的选择输入框仍会出现。 In the case that I chose text in the first few questions, although the choices don't appear at first, when the time comes that I choose either radiobox or checkbox, input boxes for choices appear underneath the previous questions where the selected answer type is text when they previously weren't there.如果我在前几个问题中选择了文本,虽然一开始没有出现选项,但是当我选择单选框或复选框时,选项的输入框会出现在前面的问题下方,其中选择的答案类型是当他们以前不在那里时的文本。

Here's the html part of the code:这是代码的html部分:

<div class="container" style="margin-top: 5%">
        <div class="row">
            <div class="col-md-12">
                <div class="panel panel-default">
                    <div class="panel-heading">Create New Survey:</div>
                        <div class="panel-group">
                            <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" class="form-control" style="height: 700px; border: none" id="survey-form">
                                <label>Survey Title: </label>
                                    <input name="survey_title" id="survey_title" type="text" class="form-control input-group"/>
                                    <input name="qnum" id="qnum" type="hidden" value=""/>
                                    <input name="choicenum" id="choicenum" type="hidden" value=""/>
                                <label>Survey Questions:</label>
                                <div id="questions">
                                </div>
                                <button class="btn btn-primary" type="button"style="display: block; margin-top: 5px;" id="addq"><span class="glyphicon glyphicon-plus"></span>Add a question</button>
                                    <button class="btn btn-success" type="submit" id="uploadsurvey" name="uploadsurvey" style="display: block; margin-top: 10px">Upload Survey</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>

This is the js part:这是js部分:

    $(document).ready(function(){
    var i=0;
    var numOfQuestions=0;
    var choice_c = 1;
    $("#addq").click(function(){
            numOfQuestions++;
            i++;
            $("#questions").append("<div id='newq' style='margin-top: 5px; margin-bottom: 5px'></div>");
            $("#newq").append("Question: <input type='text' name='q"+i+"' class='form-control' style='display: inline-block' id='q"+i+"'/>");
            $("#qnum").attr("value", numOfQuestions);
            $("#newq").append("<label><input type='radio' name='radio"+i+"' value='radio' class='choices radiobox' id='radiobox"+i+"'/>&nbsp;Radiobox&nbsp;&nbsp;</label>");
            $("#newq").append("<label><input type='radio' name='radio"+i+"' value='check' class='choices checkbox' id='checkbox"+i+"'/>&nbsp;Checkbox&nbsp;&nbsp;</label>");
            $("#newq").append("<label><input type='radio' name='radio"+i+"' value='text' class='choices textbox' id='textbox"+i+"'/>&nbsp;Text&nbsp;&nbsp;</label>");
            $("#newq").append("<input type='text' class='form-control qchoice' name='radiochoice"+choice_c+"_q"+i+"' id='radiochoice"+choice_c+"' title='q"+i+"' style='display: none'/>");
            choice_c++;
            $("#newq").append("<input type='text' class='form-control qchoice' name='radiochoice"+choice_c+"_q"+i+"' id='radiochoice"+choice_c+"' title='q"+i+"' style='display: none'/>");
            choice_c++;
            $("#newq").append("<button id='radiobtn"+i+"' type='button' class='btn btn-primary add-radio-choice' style='display: none'><span class='glyphicon glyphicon-plus'></span>Add choices</button>");
            $("#newq").append("<button id='checkbtn"+i+"' type='button' class='btn btn-primary add-checkbox-choice' style='display: none'><span class='glyphicon glyphicon-plus'></span>Add choices</button>");
        });
        $(document.body).on('change', '.choices' ,function() {
            if ($(".radiobox").is(":checked")) {
                $(".add-radio-choice").show();
                $(".qchoice").show();
                $(".add-checkbox-choice").hide();
            }
            else if ($(".checkbox").is(":checked")) {
                $(".add-checkbox-choice").show();
                $(".add-radio-choice").hide();
                $(".qchoice").show();
            }
            else if($(".textbox").is(":checked")){
                $(".add-checkbox-choice").hide();
                $(".add-radio-choice").hide();
                $(".qchoice").hide();
            }
        });
        $(document.body).on('click', '.add-radio-choice' ,function(){
            $("#newq").append("Choice: <input type='text' name='radiochoice"+choice_c+"_q"+i+"' title='q"+i+"' class='form-control qchoice' id='radiochoice"+choice_c+"'/>");
            $("#choicenum").attr("value", choice_c);
        });
        $(document.body).on('click', '.add-checkbox-choice' ,function(){
            $("#newq").append("<input type='text' name='radiochoice"+choice_c+"_q"+i+"' title='q"+i+"' class='form-control qchoice' id='checkboxchoice"+choice_c+"'>");
            $("#choicenum").attr("value", choice_c);
        });
});

I also added a fiddle for this one here :我还在这里为这个添加了一个小提琴:

I'm also supposed to add an option for dynamically deleting choices and questions in the form, but I don't know how to work around it yet...我还应该添加一个选项来动态删除表单中的选项和问题,但我还不知道如何解决它...

Well, the first thing, your fiddle doesn't include either jQuery or the bootstrap CSS connections.好吧,首先,您的小提琴不包含 jQuery 或引导 CSS 连接。 But that's minor.但那是次要的。 Second thing, you may be getting yourself confused by putting everything in a single function.其次,将所有内容都放在一个函数中可能会让您感到困惑。 Instead, let sub-functions handle some of the work for you.相反,让子功能为您处理一些工作。

As to your problem with the radio still showing when you select the text option, take a look below or at this fiddle .至于当您选择文本选项时收音机仍然显示的问题,请看下面或这个小提琴 When the user selects a different answer option, simply remove the DOM structure to handle the old option and create the DOM structure for the new.当用户选择不同的答案选项时,只需删除 DOM 结构来处理旧选项并为新选项创建 DOM 结构。

I've tried to comment the code pretty extensively, should be fairly clear -- but ask questions if you have 'em!我试图对代码进行相当广泛的评论,应该相当清楚——但如果你有问题,请提出问题!

 $(document).ready(function() { var i = 0; var numOfQuestions = 0; var labelEl = $("<label>"); var inputEl = $("<input>"); // Handle the user clicking on "Add a question" $("#addq").click(function() { // Increment both counters, numOfQuestions++; i++; // a nested function creates the HTML DOM structure. addQuestion(); // Handle the user choosing what type of question they want $(".choices").on("change", function() { var option = $(this).val(); switch (option) { case "radio": showRadioOpts(); break; case "checkbox": showCheckboxOpts(); break; case "text": showTextOpts(); break; } }) }); /** * This handles the HTML DOM creation. I don't want to clog up * the main routine with all the ugly, so I've moved it here. * Purely cosmetic. The functioning is the same as the former * append() functions with the element completely spelled out. **/ function addQuestion() { var newQuestionEl = inputEl.clone().prop({ "type": "text", "name": "q" + i, "id": "q" + i, "class": "form-control" }); var newQuestion = labelEl.clone().prop({ "for": "q" + i, "class": "form-control" }).append("Question: ", newQuestionEl); var newQTypeArr = []; var newQTypeRadioEl = inputEl.clone().prop({ type: "radio", name: "qType" + i, id: "qType" + i, value: "radio", class: "choices radiobox" }); newQTypeArr[0] = labelEl.clone().append(newQTypeRadioEl, " Radio"); var newQTypeCheckEl = inputEl.clone().prop({ type: "radio", name: "qType" + i, id: "qType" + i, value: "checkbox", class: "choices radiobox" }); newQTypeArr[1] = labelEl.clone().append(newQTypeCheckEl, "Checkbox"); var newQTypeTextEl = inputEl.clone().prop({ type: "radio", name: "qType" + i, id: "qType" + i, value: "text", class: "choices radiobox" }); newQTypeArr[2] = labelEl.clone().append(newQTypeTextEl, "Text"); var answerOptionsEl = $("<div>").prop({ class: "answer-options-pane" }); var newAnsContainerEl = $("<div>").prop({ class: "answer-pane" }).append(newQTypeArr, answerOptionsEl); var newQContainerEl = $("<div>").prop({ id: "newq" }).append(newQuestion, newAnsContainerEl); $("#questions").append(newQContainerEl); $("#qnum").attr("value", numOfQuestions); }; //end addQuestion() // Toggle the radio answer options function showRadioOpts() { // First, hide all answer options. Then, add one radio option. $(".answer-options-pane").empty(); addRadioOpts(); }; // end showRadioOpts() // Toggle the checkbox answer options function showCheckboxOpts() { $(".answer-options-pane").empty(); }; // end showCheckboxOpts() // Toggle the text box answer options function showTextOpts() { $(".answer-options-pane").empty(); addTextOpts(); }; // end showTextOpts() /*** * Another DOM element creation function. This creates the radio * button text option, and if it's the first, a button to add * more options. ***/ function addRadioOpts() { // We want to get the length of the current choices, // as this will give us an index for the new option var radioChoice = $(".radio-choice"); var choice_c = radioChoice.length; var radioChoiceTextEl = inputEl.clone().prop({ "type": "text", "class": "form-control answer-option radio-choice", "name": "radiochoice" + choice_c + "_q" + i, "id": "radiochoice" + choice_c + "_q" + i, "title": "q" + i }); // If we don't have any radio elements yet, we will also // want to create an "add more options" button. if (choice_c <= 0) { var addIconEl = $("<span>").prop({ "class": "glyphicon glyphicon-plus" }); var addChoiceButton = $("<button>").prop({ "id": "radiobtn" + i, "class": "btn btn-primary add-radio-choice answer-option" }).append(addIconEl, "Add choices").on("click", function(evt) { // Make sure you don't let that button do what buttons do... evt.preventDefault(); addRadioOpts() }); $(".answer-options-pane").append(addChoiceButton); } radioChoiceEl = labelEl.clone().append(radioChoiceTextEl); // Make sure to add the new text element BEFORE the // add more button. $(".add-radio-choice").before(radioChoiceEl); }; function addTextOpts(){ var textChoiceTextEl = inputEl.clone().prop({ "type": "text", "class": "form-control answer-option text-choice", "name": "radiochoice_q" + i, "id": "textchoice_q" + i, "title": "q" + i }); textChoiceEl = labelEl.clone().append("Text: ", textChoiceTextEl); $(".answer-options-pane").append(textChoiceEl) } });
 label { display: block; }
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container" style="margin-top: 5%"> <div class="row"> <div class="col-md-12"> <div class="panel panel-default"> <div class="panel-heading">Create New Survey:</div> <div class="panel-group"> <form action="<?php $_SERVER['PHP_SELF']; ?>" method="POST" class="form-control" style="height: 700px; border: none" id="survey-form"> <label>Survey Title: </label> <input name="survey_title" id="survey_title" type="text" class="form-control input-group" /> <input name="qnum" id="qnum" type="hidden" value="" /> <input name="choicenum" id="choicenum" type="hidden" value="" /> <label>Survey Questions:</label> <div id="questions"> </div> <button class="btn btn-primary" type="button" style="display: block; margin-top: 5px;" id="addq"><span class="glyphicon glyphicon-plus"></span>Add a question</button> <button class="btn btn-success" type="submit" id="uploadsurvey" name="uploadsurvey" style="display: block; margin-top: 10px">Upload Survey</button> </form> </div> </div> </div> </div> </div> </div>

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

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