如何通过serializearray()获取复选框值

时间:2014-01-15 12:26:36

标签: javascript php jquery serialization

如何以[{abc:[1,2,3]}]

等数组格式获取复选框值

现在我变得像[{abc[]:1,abc[]:2,abc[]:3}] ...

获取serializedarray我正在使用var form = $('.wpc_contact').serializeArray();

这是我的html表单,它可以生成动态(我使用拖放创建动态表单)

    <form method="POST" name="1" class="form-horizontal wpc_contact" novalidate="novalidate">
<fieldset>
    <div id="legend" class="">
        <legend class="">Demo</legend> 
        <div id="alert-message" class="alert hidden" style="color: red;"></div>
    </div>

    <div class="control-group">
        <label class="control-label">Checkboxes</label>
        <div class="controls" name="wpc_chkbox" req="yes">
            <label class="checkbox"> 
                <input type="checkbox" value="Option one" id="wpc_chkbox_0" name="wpc_chkbox[]" req="yes">  Option one        
            </label>        
            <label class="checkbox">
                <input type="checkbox" value="Option two" id="wpc_chkbox_1" name="wpc_chkbox[]" req="yes">   Option two
            </label>    
        </div>
        <p class="help-blocksp" style="display:none;">wpc_chkbox</p>
        <p class="help-block1" style="display:none;" checked="checked"></p>
    </div>
    <div class="control-group">
        <label class="control-label">Inline Checkboxes</label>
        <div class="controls" name="wpc_inline_chkbox" req="yes">
            <label class="checkbox inline">
                <input type="checkbox" value="1" name="wpc_inline_chkbox[]" id="wpc_inline_chkbox_0" req="yes"> 1
            </label>
            <label class="checkbox inline">
                <input type="checkbox" value="2" name="wpc_inline_chkbox[]" id="wpc_inline_chkbox_1" req="yes"> 2
            </label>
            <label class="checkbox inline">
                <input type="checkbox" value="3" name="wpc_inline_chkbox[]" id="wpc_inline_chkbox_2" req="yes"> 3
            </label>
        </div>      
        <p class="help-block" style="display:none;">wpc_inline_chkbox</p> 
        <p class="help-block1" style="display:none;" checked="checked"></p>
    </div>

    <div class="control-group">      
        <div class="controls">                          
            <button class="btn btn-success">Button</button>                     
        </div>                  
    </div>
</fieldset>                 

1 个答案:

答案 0 :(得分:0)

要创建数组,请使用此 -

var arr = $("input[name='checkboxname[]']:checked").map(function() { 
            return this.value; 
          }).get();

Alernate解决方案:

<input type="checkbox" class="selector" value="{value}"/> //add as many as you wan't
JS

 var checked='';
            $('.selector:checked').each(function(){
                checked=checked+','+$(this).val();
            });
PHP

 $ids=explode(',',substr($_GET['param_with_checked_values'],1));