2010-11-02 70 views
5

Tôi có danh sách các hộp kiểm sau đây. Cách danh sách là đầu ra nó cho phép người dùng chọn nhiều hơn một giá trị. Tuy nhiên, tôi cần người dùng chỉ được phép chọn một giá trị duy nhất từ ​​danh sách hộp kiểm bên dưới.Hộp kiểm chuyển đổi (nhiều lựa chọn) thành nút radio hoặc hộp kiểm có lựa chọn duy nhất?

Có thể bằng cách nào đó đạt được điều này với jQuery không? Cũng có thể thay đổi danh sách thành các nút radio, chọn danh sách hoặc bất kỳ thứ gì khác có thể hữu ích.

Rất tiếc, tôi không có quyền truy cập vào chức năng tạo, vì vậy tôi không thể thay đổi đầu ra.

Cảm ơn trước

<form class="ajax-form" id="user-register" method="post" accept-charset="UTF-8" action="(...)"> 

(...) 

<div id="edit-notifications-custom-custom-berlin-wrapper" class="form-item form-option"> 
    <label for="edit-notifications-custom-custom-berlin" class="option"><input type="checkbox" class="form-checkbox" value="1" id="edit-notifications-custom-custom-berlin" name="notifications_custom[custom_berlin]"> Berlin</label> 
</div> 

<div id="edit-notifications-custom-custom-hamburg-wrapper" class="form-item form-option"> 
    <label for="edit-notifications-custom-custom-hamburg" class="option"><input type="checkbox" class="form-checkbox" value="1" id="edit-notifications-custom-custom-hamburg" name="notifications_custom[custom_hamburg]"> Hamburg</label> 
</div> 

<div id="edit-notifications-custom-custom-frankfurt-am-main-wrapper" class="form-item form-option"> 
    <label for="edit-notifications-custom-custom-frankfurt-am-main" class="option"><input type="checkbox" class="form-checkbox" value="1" id="edit-notifications-custom-custom-frankfurt-am-main" name="notifications_custom[custom_frankfurt-am-main]"> Frankfurt am Main</label> 
</div> 

<div id="edit-notifications-custom-custom-duesseldorf-wrapper" class="form-item form-option"> 
    <label for="edit-notifications-custom-custom-duesseldorf" class="option"><input type="checkbox" class="form-checkbox" value="1" id="edit-notifications-custom-custom-duesseldorf" name="notifications_custom[custom_duesseldorf]"> Düsseldorf</label> 
</div> 

<div id="edit-notifications-custom-custom-bielefeld-wrapper" class="form-item form-option"> 
    <label for="edit-notifications-custom-custom-bielefeld" class="option"><input type="checkbox" class="form-checkbox" value="1" id="edit-notifications-custom-custom-bielefeld" name="notifications_custom[custom_bielefeld]"> Bielefeld</label> 
</div> 

<div id="edit-notifications-custom-custom-muenchen-wrapper" class="form-item form-option"> 
    <label for="edit-notifications-custom-custom-muenchen" class="option"><input type="checkbox" class="form-checkbox" value="1" id="edit-notifications-custom-custom-muenchen" name="notifications_custom[custom_muenchen]"> München</label> 
</div> 

(...) 

asdasd

+0

Nếu bạn có thể, nút radio là con đường để đi. Không cần phải thêm mã phía máy khách phức tạp để đạt được thứ gì đó vốn có sẵn trong HTML. – dotariel

+0

Tôi đồng ý, nhưng như tôi đã nói tôi không thể thay đổi điều này trước khi nó được xuất. – maze

Trả lời

-1

Hãy thử điều này:

$('[type=checkbox]').attr('type','radio'); 
+0

Bạn không thể thay đổi loại đầu vào khi nó được thêm vào DOM. – shoebox639

+0

crap ... đủ công bằng. – jellyfishtree

+0

có thể sử dụng .insertSau khi chèn một radio có cùng chi tiết sau nó và sau đó xóa – jellyfishtree

1

Cách đẹp nhất để làm điều này sẽ được thay đổi kiểu trong HTML gốc. Nếu bạn không thể làm được điều này, tuy nhiên, bạn có thể làm như sau:

$(document).ready(function(){ 
    $('input.form-checkbox').change(function(){ 
     $('input.form-checkbox').not(this).each(function(idx, el){ 
      el.checked = false; 
     }); 
    }); 
}); 
3
$('.form-checkbox').bind('click', function() { 
    $('.form-checkbox').not(this).attr('checked', false); 
}); 

chỉnh sửa để tối ưu hóa hiệu suất.

+0

Bạn có thể đơn giản hóa điều này thành '$ ('. Form-checkbox') .không (this) .attr ('checked', false ')', sẽ làm tăng đáng kể hiệu suất. [attr() là chậm hơn rất nhiều so với mỗi() trong trường hợp này] (http://jsperf.com/attr-vs-each), tuy nhiên. – lonesomeday

+0

Cảm ơn! Sử dụng .each đã trở thành gần như máy móc với tôi. heh. – shoebox639

+0

Thật là một giải pháp gọn gàng! Thx –

1

mã này thay đổi mã quá trình

$(document).ready(function(){ 
    $('input.form-checkbox').each(function(){ 
     var self=$(this); 
     $(this).replaceWith(
      $('<input type="radio" />'). 
       attr("value",self.attr("name")). 
       attr("name","notifications_custom"). 
       attr("id",self.attr("id")). 
       addClass("form-radio") 
     ); 
    }); 
}); 
Các vấn đề liên quan