例如有两道多选题,我要获取这两道多选题的答案,并且结果是:第一题:AB 第二题:CB类似这种形式的。
下面的方法我能得到两道题的答案,但是不能把两题选择的结果分开,以便我之后去获取相应题目的结果,请问有什么好的方法可以解决这个问题???
var str="";
$([name='checkbox']:checked).each(function(){
   str += $(this).val();
})

解决方案 »

  1.   

    $([name='checkbox1']:checked).each(function(){
    加个 编号 区分第n道题
      

  2.   


    function fn(){
         var result = {};
         $("[type='checkbox']").each(function(){
         var $this = $(this),
         name = $this.attr("name");
         if ($this.is(":checked")){
         result[name] ? result[name] += $this.val() : result[name] = $this.val();
         }
        
         });
                    alert(result )    
         }1,<input type="checkbox" name="c1" value="a">a
       <input type="checkbox" name="c1" value="b">b
       <br>
       2,<input type="checkbox" name="c2" value="a">a
       <input type="checkbox" name="c2" value="b">b
       <br>
       3,<input type="checkbox" name="c3" value="a">a
       <input type="checkbox" name="c3" value="b">b
       <a href="#" onclick="fn()">查看成绩
      

  3.   

    <head>
        <title>Test</title>
        <script src="jquery.js"></script>
    </head>
    <body>
    <form>
    <div id='Q1' class="question">
    <input type="checkbox" id="tel"/>
    <label>A</label>
    <input type="checkbox" id="tel"/>
    <label>B</label>
    <input type="checkbox" id="tel"/>
    <label>C</label>
    <input type="checkbox" id="tel"/>
    <label>D</label>
    </div>
    <div id='Q2' class="question">
    <input type="checkbox" id="tel"/>
    <label>A</label>
    <input type="checkbox" id="tel"/>
    <label>B</label>
    <input type="checkbox" id="tel"/>
    <label>C</label>
    <input type="checkbox" id="tel"/>
    <label>D</label>
    </div>
    <input type="button" value="button" id="btn" />
    </form>
    <script type="text/javascript" >
    var $form = $('form');
    var values = [];
    $form.find('#btn').click(function(){
    $form.find('.question').each(function(index){
    var str = '';
    $(this).find('[type=checkbox]:checked').each(function(){
    str += $(this).next().text();
    //values[index][i] = $(this).next().text();
    });
    values[index] = str;
    });
    $.each(values,function(i,v){
    alert(i+": "+v);
    });
    });
    </script>
    </body>
    </html>
      

  4.   

    请问一下,如果这个地方$(this).find('[type=checkbox]:checked').each不仅仅是多选框,还有textarea,那find里可不可以有多个参数呢?(因为操作的代码是一样的,我不想重复写)
      

  5.   

    $(this).find('[type=checkbox]:checked,textarea')