如何通过jQuery或JavaScript中的选中复选框获取数组?

时间:2013-02-16 11:42:01

标签: javascript ajax jquery cakephp

我想获取一组用户选中的复选框,例如,如果用户选择product1,而在product1中,电子邮件是@ acom,如果用户选择了许多复选框,我想要他选择的数组中的所有电子邮件数据反对产品。数据将代表id,我被困在这里,努力解决该问题2天但未获得成功:

      <?php v_start($this);?>
    <style>
    .highlight{ font-wieght:bold;   color:#F00;}
    .amounts{}
    .tick{ float:left; margin-top:-5px;}    
    .highlight .amount{ display : inline;}
    .tick img{  display:none;   width   : 25px; height  : 25px; margin-right: 10px; float   : left;}
    .highlight .tick img{   display : block;}
    </style>


    <script>
    jQuery(document).ready(function (){

        jQuery('.amounts').click(function (){
            var pname = jQuery(this).attr('product_name');
            if(!jQuery(this).hasClass('highlight')){
                jQuery('.amount-'+pname).removeClass('highlight');
                jQuery(this).addClass('highlight');
            }
            else {
                jQuery(this).removeClass('highlight');
                }           
        });

    });
    </script>
    <h1><?php echo __l('Step 1 ')?>-> <span style="color:red"><?php echo __l('Step 2')?></span></h1>

    <?php echo $this->FormManager->create('User',array_merge($form_options,array('default'=>true)));?>

        <table>
            <thead>
                <tr cellpadding="0" cellspacing="0" width="100%" >
                    <?php    foreach($name as $product) { foreach ($product as $key) { 
         for($i=0;$i<sizeof($key['ProductPlan']);$i++) {
                        ?>
                    <th>
                        <?php echo $key['Product']['name']?>
                        <?php echo $this->FormManager->input($key['Product']['name'],array('type'=>'hidden','id'=>$key['Product']['name'],'class'=>'hid'))?> 
                    </th>
                    <?php  }}?>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <?php 
                        foreach($name as $product) {
                            $c = 0;
                    ?>

                        <?php 
                            foreach ($product as $key) {
                                $c++;
                        ?>
                            <td>
                          <?                                                                                                        for($i=0;$i<sizeof($key['ProductPlan']);$i++)    { ?>
                                <div 
                                     class   = 'packages'
                                     id      = "<?php echo $key['Product']['name'],$key['ProductPlan'][$i]['product_plan_id']?>" 
                                     onclick = "document.getElementById('<?php echo $key['Product']['name']?>').value='<?php echo $key['ProductPlan'][$i]['product_plan_id']?>';"
                                >

                                    <div product_name='<?php echo $key['Product']['slug']?>' class='amounts amount-<?php echo $key['Product']['slug']?>'>
                                        <div class='tick'>
                                            <?php echo $this->Html->image('tick.png', array('width'=>'25', 'height'=>'25'));?>
                                        </div>

                                        <div class='amount'>
                                            <?php echo $key['ProductPlan'][$i]['name'];?> 
                                            </br></br>
                                        </div>
                                        <div>Create Up To:<?php echo $key['ProductPlan'][$i]['limit'];?></div></br>
                                        <div>Product Plan Amount:<?php echo $key['ProductPlan'][$i]['product_plan_amount'];?>.$</div></br>
                                    </div>
                                </div>
                                <?php }?>

                        <?php } ?>
                    <?php } ?>


                            </td>
                </tr>
<tr>        
</tr>
            </tbody>
        </table>    

2 个答案:

答案 0 :(得分:0)

如果您可以提供HTML输出而不是PHP源,那么它将更容易为您提供帮助。

如果您想要定位所有已选中的复选框,或者可能是他们的相关电子邮件输入字段,您可以使用以下内容:

var email_arr = new Array();
$('input:checkbox:checked').each(function() { //goes through each checked input box
    email_arr.push($(this).siblings("input.email").val()); //Gets the value of an email input field
});

重要的是你如何放置电子邮件输入字段。如果它与复选框不在同一元素中,则可能需要使用.parent或.parents遍历一个或两个节点。

希望有所帮助,

//空翻

答案 1 :(得分:0)

我不确定我明白你在问什么,这个解决方案有帮助吗?

http://jsfiddle.net/rG6bF/1/

$(":checkbox").click(function(e) {
    $.each($(":checked"),function(idx,chkbox) {
        log($(chkbox).attr('id'));
    });
});