2015-05-27 20 views
1

這是我的代碼。我想要的是,當我選擇複選框時,其他複選框將取消選中...像單選按鈕一樣的行爲..但使用td類...是否有可能在jQuery中?當用戶選擇某個checbox時取消選中所有文本框

<table> 
    <tr> 
     <td class="example"> 
      <input type="checkbox" /> 
     </td> 
     <td class="example"> 
      <input type="checkbox" /> 
     </td> 
     <td class="example"> 
      <input type="checkbox" /> 
     </td> 
     <td class="example"> 
      <input type="checkbox" /> 
     </td> 
    </tr> 
</table> 

在此先感謝..

+1

顯示jquery/javascript代碼你已經試過?? –

+0

爲什麼你不使用無線電輸入呢? –

+0

CMS和樣機設計問題...樣機設計看起來像這樣沒有選擇...而我使用的CMS有點難以說到單選按鈕。 –

回答

3

您可以使用:

$(":checkbox").change(function(){ 
    $(this).closest('tr').find('input').not(this).prop('checked', false); 
}); 
1

對不起,麻煩的傢伙......我已經知道了..

這裏的代碼.. 。

$('.example input').on('click', function() { 
    $('.example input').not(this).prop('checked', false); 
}); 
+0

你需要檢查複選框是否被選中,http://stackoverflow.com/a/30476090/2025923 – Tushar

2

試試這個:

$('table').on('click', ':checkbox', function() { 
// On click of any checkbox inside the table 

    // If checkbox is checked then only uncheck others 
    if ($(this).is(':checked')) { 
     $('table').find(':checkbox').not(this).prop('checked', false); 
    } 
}); 

演示:http://jsfiddle.net/tusharj/yz5yjwwu/

+0

這行不是必須的if($(this).is(':選中')){' –

+1

優化,當點擊相同的複選框 – Tushar

+0

是的,你是對的 –

2

嘗試

$('.example').click(function() { 
    $('.example').find('input[type=checkbox]').prop('checked', false); 
    $(this).find('input[type=checkbox]').prop('checked', true); 
}); 

Fiddle

1

就是這樣:)

  $("input[type='checkbox']").on("change",function(){ 
 
       $("input[type='checkbox']").prop("checked",false); 
 
       $(this).prop("checked",true); 
 
     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <tr> 
 
     <td class="example"> 
 
      <input type="checkbox" /> 
 
     </td> 
 
     <td class="example"> 
 
      <input type="checkbox" /> 
 
     </td> 
 
     <td class="example"> 
 
      <input type="checkbox" /> 
 
     </td> 
 
     <td class="example"> 
 
      <input type="checkbox" /> 
 
     </td> 
 
    </tr> 
 
</table>

1

使用以下代碼。工作DEMO

$(":checkbox").on('click',function(){ 
    if($(this).is(':checked')){ 
     $('td.example :checkbox').prop("checked",false); 
     $(this).prop("checked",true); 
    } 
}); 
相關問題