如何通过jQuery或JavaScript中选中的复选框获取数组?
我希望获得用户选择的复选框数组,例如,如果用户选择产品1,则在产品1中电子邮件是a@acom像这样,如果用户选择了许多复选框,我希望所有电子邮件数据都在他根据产品选择的数组中。数据将代表id获取,我被困在这里,为解决该问题工作了2天,但没有成功:如何通过jQuery或JavaScript中选中的复选框获取数组?,javascript,ajax,jquery,cakephp,Javascript,Ajax,Jquery,Cakephp,我希望获得用户选择的复选框数组,例如,如果用户选择产品1,则在产品1中电子邮件是a@acom像这样,如果用户选择了许多复选框,我希望所有电子邮件数据都在他根据产品选择的数组中。数据将代表id获取,我被困在这里,为解决该问题工作了2天,但没有成功: <?php v_start($this);?> <style> .highlight{ font-wieght:bold; color:#F00;} .amounts{} .tic
<?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>
如果您可以提供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向上遍历一两个节点
希望有帮助
//翻筋斗我不确定我是否理解你的要求,这个解决方案有帮助吗
你的复选框是…?@axel.michel复选框通过输入调用的。我在你的代码中看到的唯一输入是hidden类型。你正在重新发布你的代码:,如果你对你得到的答案不满意,你应该考虑你问什么和怎么问。看看下面的答案,没人知道你想要什么。据我所知,你甚至没有复选框。解决你的问题。
$(":checkbox").click(function(e) {
$.each($(":checked"),function(idx,chkbox) {
log($(chkbox).attr('id'));
});
});