选中一个复选框后,如何禁用其他复选框?(续订)

时间:2013-03-04 06:51:15

标签: php javascript checkbox

明确提出问题

使用sql server 2008 enterprise作为数据库服务器 检查我的代码

表格视图

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<form method="post" name="form" id="checkForm" action="update.php">

<?php
$aksi="aksi_view.php";
switch($_GET[act]){
  // Tampil User
  default:

    echo "
    <table>
    <tr><th>Ap</th><th>Ccl</th><th>No</th><th>Budg</th><th>Dept</th><th>ItemID</th><th>Item Name</th><th>Qnty</th><th>UOM</th><th>Curr.</th><th>Rate</th><th>Unit Price</th>
    <th>Total</th><th>Total-IDR</th></tr>";

    $p      = new Paging;
    $batas  = 20;
    $posisi = $p->cariPosisi($batas);

    $counter = 1;

    $sql = "SELECT TOP 20 BudgetCategoryID,DeptAbbr, BudgetCategoryAbbr, ItemID, Status,
                            QntyDept, Qnty, CurrencyID, UnitPrice, ExchangeRateIDR, Remark,Proposal,
                            DeptApprovalRemark, M2ApprovalBy, M2ApprovalRemark, ItemName, PurchaseUOMName,
                            DecimalInQnty,(Qnty*UnitPrice) as Total, TotalIDR, CompanyDeptStock, CompanyDeptID, OutstandingNPBB
                            FROM vwBudgetApproval
                            WHERE (M2ApprovalBy IS NOT NULL OR (M2ApprovalBy IS NULL AND  Status =1)) AND 
                            CompletedInd = 1 AND DeptApprovalBy IS NOT NULL 
                            ORDER BY BudgetCategoryID, ItemID";
    $tampil = mssql_query($sql) ;

$no = $posisi+1;

while($r=mssql_fetch_array($tampil)){
  // echo"$no=2 '-'"; 
   //echo"$r[ItemID]";
      $ExchangeRateIDR = number_format($r['ExchangeRateIDR'],2,',','.');
    $unitprice = number_format($r['UnitPrice'],2,',','.'); 
    $total = number_format($r['Total'],2,',','.');
    $totalidr = number_format($r['TotalIDR'],2,',','.');


    $warnaGenap = "#66CCFF";   // warna abu-abu
    $warnaGanjil = "#FFFFFF";  // warna putih

if ($counter % 2 == 0) $warna = $warnaGenap;
else $warna = $warnaGanjil;


echo "<tr bgcolor='".$warna."'>            


 <td><input type= 'checkbox' name='approve".$no."' id='approve'   value='".$r['ItemID']."' onclick='onClickFn()'></td>
                    <td><input type= 'checkbox' name='cancel".$no."' id='cancel'   value='".$r['ItemID']."' onclick='onClickFn()'></td> 
                    <td>$no</td>
                    <td>$r[BudgetCategoryAbbr]</td>
                    <td>$r[DeptAbbr]</td>
                    <td>$r[ItemID]</td>
                    <td>$r[ItemName]</td>
                    <td>$r[Qnty]</td>
                    <td>$r[PurchaseUOMName]</td>
                    <td>$r[CurrencyID]</td>
                    <td>$r[ExchangeRateIDR]</td>
                    <td>$unitprice </td>
                    <td>$total</td>
                    <td>Rp $totalidr</td>";             
          $no++;
          $counter++;

        }
    echo "</table>";
?>

显示页面

images

现在我希望用户在选中一个复选框时选中另一个复选框禁用 我使用java脚本的功能。这是我的代码

<script type="text/javascript">
function onClickFn(){
$("approve").click(function(){
    if ($(this).prop("checked"))
    {
        $("cancel").prop("checked",false);
    }
});

$("cancel").click(function(){
    if ($(this).prop("checked"))
    {
        $("approve").prop("checked",false);
    }
})
};
</script>

但该功能不起作用。

3 个答案:

答案 0 :(得分:2)

1. First of all you have given same id to all <TD>.
2. You haven't used proper id reference in your javascript.
3. Use radio button instead.

答案 1 :(得分:1)

jQuery的:

$('table').on('click', 'input:checkbox', function() {
    $(this)
    .parents('tr')
    .find('input:checkbox:not(:checked)')
    .attr('disabled', $(this).is(':checked'));
});

http://jsfiddle.net/samliew/ERZge/

但是,您确实应该使用单选按钮,除非当然,您希望用户能够取消选中复选框。

答案 2 :(得分:0)

为什么不用单选按钮?这似乎是一个更好的选择。 此外,如果你想要禁用单选按钮,这就是jQuery的样子: jQuery("input:radio").attr('disabled',true);