2013-02-16 71 views
-2

我想獲得用戶選擇複選框的數組,例如,如果用戶選擇product1,並且在product1中電子郵件是@acom那樣,如果用戶選擇多個複選框我希望他選擇的數組中的所有電子郵件數據產品。這些數據將得到代表的ID,我卡在這裏,努力解決該問題2天,但沒有獲得成功:如何通過jQuery或JavaScript中的選中複選框來獲取數組?

 <?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>  
+0

而你的複選框在哪裏...? – 2013-02-16 11:49:07

+0

@ axel.michel複選框通過輸入呼叫 – usii 2013-02-16 11:52:50

+0

只有輸入我可以在您的代碼中看到的是類型隱藏 – 2013-02-16 11:57:58

回答

0

我不知道我理解你的要求,將這一解決方案的幫助?

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

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

好人......你真的關閉了。 – usii 2013-02-16 12:22:47

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來遍歷一個或兩個節點。

希望幫助,

//空翻

相關問題