Lấy giá trị của các ô check box 2007-08-21 04:33:27

Trong 1 list các check box và lấy giá trị của những ô được checked.
Demo:<script>function BASIC_SelectItem(){var c_value = "";for (var i=0; i < document.form_id.chk.length; i++){if (document.form_id.chk[i].checked){c_value += (c_value?',':'')+document.form_id.chk[i].value;}} alert(c_value);}</script>
<form id="form_id" name="form_id" method="post" action="">
      1 <input type="checkbox" name="chk" id="chk[]" value="1" />
      2 <input type="checkbox" name="chk" id="checkbox2" value="2" />
      3 <input type="checkbox" name="chk" id="checkbox4" value="3" />
      4 <input type="checkbox" name="chk" id="checkbox3" value="4" />
      5 <input type="checkbox" name="chk" id="checkbox5" value="5" />
      <input type="button" name="button" id="button" value="Button" onclick="BASIC_SelectItem();" />
</form>


<script>
function BASIC_SelectItem()
{
var c_value = "";
for (var i=0; i < document.form_id.chk.length; i++)
   {
   if (document.form_id.chk[i].checked)
      {
      c_value += (c_value?',':'')+document.form_id.chk[i].value;
      }
   }
  return c_value;
}</script>
<form id="form_id" name="form_id" method="post" action="">
<table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC">
  <tr>
    <td align="center" bgcolor="#FFFFFF">
      <input type="checkbox" name="chk" id="chk[]" value="1" />
      1</td>
    <td align="center" bgcolor="#FFFFFF"><input type="checkbox" name="chk" id="checkbox2" value="2" />
      2</td>
    <td align="center" bgcolor="#FFFFFF"><input type="checkbox" name="chk" id="checkbox4" value="3" />
      3</td>
    <td align="center" bgcolor="#FFFFFF"><input type="checkbox" name="chk" id="checkbox3" value="4" />
      4</td>
    <td align="center" bgcolor="#FFFFFF"><input type="checkbox" name="chk" id="checkbox5" value="5" />
      5
      <input type="button" name="button" id="button" value="Button" onclick="BASIC_SelectItem();" /></td>
  </tr>
</table>

</form>

Tra loi 5 comment(s) TG 2007-08-21 04:33:27

toiyeuphp 2007-08-21 08:47:15

hic http://www.webdevelopersnotes.com/tips/html/finding_the_value_of_a_checkbox.php3

Tra loi

nghikhac 2007-10-11 04:41:40

kho hieu qua di bac TG a,sao bac ko dung getElementsByName.

Tra loi

nghikhac 2007-10-11 04:43:52

<html>
<head>
<script type="text/javascript">
function check_Box()
{
var c_value="";
for(i=0;i<document.getElementsByName('checkbox').length;i++)
{
if(document.getElementsByName('checkbox')[i].checked)
{
c_value+=(c_value?',':'')+document.getElementsByName('checkbox')[i].value;
}
}
alert(c_value);
}
</script>
</head>
<body>
<form method="post" action="" name="form_" id="form_">
<table width="81%" align="center" bgcolor="#CCCCCC" cellpadding="0" cellspacing="0">
<tr>
   <td width="15%"><input type="checkbox" name="checkbox" value="1">1</td>
<td width="15%"><input type="checkbox" name="checkbox" value="2">2</td>
<td width="15%"><input type="checkbox" name="checkbox" value="3">3</td>
<td width="15%"><input type="checkbox" name="checkbox" value="4">4</td>
<td width="16%"><input type="checkbox" name="checkbox" value="5">5</td>
   <td width="10%"><input type="button" name="button" id="button" value="button" onClick="check_Box();"></td>
</tr>
</table>
</form>
</body>
</html>

Tra loi

TG 2007-10-12 08:29:02

thì một bài toán có nhiều cách giải, nên bạn cứ dùng cái nào thấy tiện nhất, và nên post mấy cái kiểu vậy lên cho anh em tham khảo sau này nha :)

Tra loi

BinhMinh 2009-04-02 06:53:53

Vậy lấy giá trị của các textbox như thế nào các bác chỉ em với

Tra loi

Y kien