2010-06-25 151 views
1

我有这个跨度是在一个循环中,并产生多个结果。
我要的是当它的检查jquery复选框属性值

<span id="row" index="<?php print $i; ?>" recordID="<?php print $row->ID; ?>" Color="<?php print $row->Color; ?>"> 
<input type="checkbox" style="left:10;" /> 
</span> 

我使用这在我的js文件,以检查是否复选框被点击

var test = $('input:checkbox:checked').val();  
alert(test); 

属性的recordId值和所有我得到的是上
我怎样才能得到属性值
谢谢

回答

2

这会得到你的属性值

$('#row').attr('recordID'); 

如果你想获得当复选框被选中的值,这里有一个例子

JS文件

$(document).ready(function(){ 
$('input:checkbox').change(function(){ 
    if($(this).attr('checked')){ 
    alert($(this).parent().attr('recordID')); 
    } 
}); 
}); 

要看看有多少行已检查:

JavaScript

$(document).ready(function(){ 
$('#check').click(function(event){ 
    event.preventDefault(); 
    alert($('input:checkbox:checked').size()); 
}); 
}); 

HTML

<span id="row1" recordID="1"> 
<input type="checkbox" style="left:10;" /> 
<input type="checkbox" style="left:10;" /> 
<input type="checkbox" style="left:10;" /> 
<input type="checkbox" style="left:10;" /> 
</span> 
<span id="row2" recordID="2"> 
<input type="checkbox" style="left:10;" /> 
</span> 
<span id="row3" recordID="3"> 
<input type="checkbox" style="left:10;" /> 
</span> 
<span id="row4" recordID="4"> 
<input type="checkbox" style="left:10;" /> 
</span> 
<button id='check'>Check Num Rows</button> 

要检查单个的跨度

$(document).ready(function(){ 
    $('#check').click(function(event){ 
     event.preventDefault(); 
     alert($('#row1 input:checkbox:checked').size()); 
    }); 
}); 

这里内是我使用,使例子完整的示例代码,您需要

<html> 
<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 

<script type='text/javascript'> 
$(document).ready(function(){ 
    $('input:checkbox').change(function(){ 
     alert($(this).parent().find(':checkbox:checked').size()); 
    }); 
}); 
</script> 
</head> 
<body> 
<span id="row1" recordID="1"> 
    <input type="checkbox" style="left:10;" /> 
    <input type="checkbox" style="left:10;" /> 
    <input type="checkbox" style="left:10;" /> 
</span><br/><br/> 
<span id="row2" recordID="2"> 
    <input type="checkbox" style="left:10;" /> 
    <input type="checkbox" style="left:10;" /> 
    <input type="checkbox" style="left:10;" /> 
    <input type="checkbox" style="left:10;" /> 
</span><br/><br/> 
<span id="row3" recordID="3"> 
    <input type="checkbox" style="left:10;" /> 
    <input type="checkbox" style="left:10;" /> 
    <input type="checkbox" style="left:10;" /> 
    <input type="checkbox" style="left:10;" /> 
</span><br/><br/> 
<span id="row4" recordID="4"> 
    <input type="checkbox" style="left:10;" /> 
    <input type="checkbox" style="left:10;" /> 
    <input type="checkbox" style="left:10;" /> 
    <input type="checkbox" style="left:10;" /> 
</span> 
</body> 
</html> 
+0

我该检查点击了多少行。 谢谢 – Autolycus 2010-06-25 18:36:12

+0

我可以检查使用 alert($(“input [type = checkbox]:checked”)检查了多少个复选框。 但我希望能够只检查Span id =行。我该怎么做谢谢 – Autolycus 2010-06-25 18:40:46

+0

wowo_999谢谢你的帮助。但由于某种原因,它不能给我在一个范围内检查的盒子总数 谢谢 – Autolycus 2010-06-25 18:50:54