要实现一套调查问卷,问卷的题目是动态的,比如说如果第一个问题用户选择了A,则下一题到问题3.如果选择了B,则下一题到问题4.我想每次只显示一道题。完成问卷后一次性提交。我想用JavaScript来控制这些题目显示顺序,请教怎么实现?

解决方案 »

  1.   

    1 首先定义问题的顺序路劲,可用数组实现2 所有问题编号后放入隐藏域,或者每一道问题一个div3 根据1定义的顺序路劲,显示隐藏域或者DIV
      

  2.   

    <html>
    <head>
    <script type="text/javascript">
    var list = [{
    id: 1,
    question: '你是女生吗?',
    answers: {
    A: { text: "是",   next: 2 },
    B: { text: '不是', next: 3 }
    }
    }, {
    id: 2,
    question: '你爱溜得滑吗?',
    answers: {
    A: { text: "不爱",   next: 4 },
    B: { text: "一点点", next: 4 },
    C: { text: "爱",     next: 4 },
    D: { text: '很爱',   next: 4 }
    }
    }, {
    id: 3,
    question: '你爱菜壹零吗?',
    answers: {
    A: { text: "不爱",   next: 4 },
    B: { text: "一点点", next: 4 },
    C: { text: "爱",     next: 4 },
    D: { text: '很爱',   next: 4 }
    }
    }, {
    id: 4,
    question: '楼主可爱不?',
    answers: {
    A: { text: "是的", next: 5 },
    B: { text: '不是' }
    }
    }, {
    id: 5,
    question: '你诚实吗?',
    answers: {
    A: { text: "Not" },
    B: { text: '不是' },
    C: { text: '没有' }
    }
    }];list.forEach(function(item) {
    list["Q" + item.id] = item;
    }).length = 0;var cur = list.Q1;
    var qs = [];
    var as = [];
    var ss = [];function $(x) { return document.getElementById(x); }Array.prototype.collect = function(selector, limit) {
    var r = [];
    for (var i=0, len=this.length, limit=limit||len; i<len && r.length<limit; i++)
    if (!!selector(this[i], i, this))
    r.push(this[i]);
    return r;
    };
    Array.prototype.forEach = function(action) {
    this.collect(function(o, i, a) { action(o, i, a); }); return this;
    }
    function show() {
    $('q').innerHTML = '';
    $('q').appendChild(document.createTextNode(cur.id + "、" + cur.question));
    var as = $("a");
    as.innerHTML = '';
    for (var p in cur.answers) {
    var li = document.createElement("li");
    as.appendChild(li);
    var input = document.createElement(document.all? "<input name='radio_a'>" : "input");
    input.setAttribute("name", "radio_a");
    input.type = 'radio';
    input.value = p;
    input.id = p;

    input.onclick = function() {
    if (isNaN(cur.answers[this.value].next)) {
    $('btn_next').disabled = true;
    $('btn_submit').disabled = false;
    } else {
    $('btn_next').disabled = false;
    $('btn_submit').disabled = true;
    }
    };

    li.appendChild(document.createTextNode(p + "、"));
    li.appendChild(input);
    var label = document.createElement('label');
    label.htmlFor = input.id;
    label.appendChild(document.createTextNode(cur.answers[p].text));
    li.appendChild(label);
    }
    }
    function prev() {
    if (ss.length < 1) return;
    cur = ss.pop();
    qs.pop();
    as.pop();
    $("qs").value = qs.join(",");
    $("as").value = as.join(",");
    show();
    }
    function attach() {
    var ary = Array.prototype.collect.call(document.getElementsByName("radio_a"), function(a) { return a.checked; } );
    if (ary.length < 1) return ary; 
    qs.push(cur.id);
    as.push(ary[0].value);
    $("qs").value = qs.join(",");
    $("as").value = as.join(",");
    return ary;
    }
    function next() {
    var ary = attach();
    if (ary.length < 1) return false; 
    if (isNaN(cur.answers[ary[0].value].next)) {
    $('btn_next').disabled = true;
    $('btn_submit').disabled = false;
    } else {
    ss.push(cur);
    cur = list["Q" + cur.answers[ary[0].value].next];
    show();
    }
    }
    window.onload = show;
    function mySubmit(frm) {
    alert(frm.qs.value + "\n" + frm.as.value);
    location.replace(location.href);
    return false;
    }
    </script>
    </head>
    <body><form action="?" name="form1" onsubmit="return mySubmit(this);" id="form1" method="get">
    <input type="text" name="qs" readonly="readonly" id="qs" /><br />
    <input type="text" name="as" readonly="readonly" id="as" /><br />


    <span id="q"></span>
    <ul id="a"></ul>
    <input type="button" id="btn_prev" value="上一题" onclick="prev()" />
    <input type="button" id="btn_next" value="下一题" onclick="next()" />
    <input type="submit" id="btn_submit" value=" 提交 " onclick="attach()" disabled="disabled" />
    </form>
    </body></html>
      

  3.   

    <input name="q1" value=a type=radio onclick="next(3)">A
    <input name="q1" value=b type=radio onclick="next(4)">B
      

  4.   

    如果数据多的话, 就 ajax吧.也很容易