Làm cách nào để bỏ chọn nút radio đã chọn trong JavaScript?

Nhưng tôi sẽ có một vài nhóm nút radio trên trang của mình và nội dung html được tải qua ajax, vì vậy tôi muốn viết 1 hàm cho tất cả chúng, thay vì xác định các biến cho từng nhóm nút radio và viết cùng một chức năng cho mọi nút radio

Chỉnh sửa. cảm ơn sự giúp đỡ của bạn với các hộp kiểm, nhưng để các hộp kiểm hoạt động như một nhóm nút radio, bạn cần viết javascrip quảng cáo sẽ bỏ chọn tất cả các hộp kiểm khác có cùng tên onclick, tôi đã có nút radio css và việc thêm lớp dễ dàng hơn đối với tôi . //jsfiddle. mạng/wuAWn/9/

Ở bài trước mình đã chia sẻ cách bỏ chọn nút radio nay mình sẽ chia sẻ với các bạn cách chuyển nút radio sang bỏ chọn và kiểm tra bằng sự kiện click button trong javascript/jquery. Để xem mẫu vui lòng xem bên dưới

Để bỏ chọn một nút radio, có rất nhiều phương pháp khả dụng, nhưng chúng ta sẽ xem các phương pháp được ưu tiên nhất

ví dụ 1. Ví dụ này bỏ chọn nút radio bằng cách sử dụng thuộc tính được kiểm tra Javascript. Thuộc tính JavaScript Radio đã kiểm tra được sử dụng để đặt hoặc trả về trạng thái đã kiểm tra của Nút radio đầu vào. Thuộc tính này được sử dụng để phản ánh thuộc tính được kiểm tra HTML




 



  
    Title of the document
  
  
    
    
    
    
  
0


  
    Title of the document
  
  
    
    
    
    
  
1


  
    Title of the document
  
  
    
    
    
    
  
2



  
    Title of the document
  
  
    
    
    
    
  
3


  
    Title of the document
  
  
    
    
    
    
  
0


  
    Title of the document
  
  
    
    
    
    
  
5


  
    Title of the document
  
  
    
    
    
    
  
2



  
    Title of the document
  
  
    
    
    
    
  
7


  
    Title of the document
  
  
    
    
    
    
  
0


  
    Title of the document
  
  
    
    
    
    
  
00


  
    Title of the document
  
  
    
    
    
    
  
2



  
    Title of the document
  
  
    
    
    
    
  
02


  
    Title of the document
  
  
    
    
    
    
  
03



  
    Title of the document
  
  
    
    
    
    
  
7____105____100


  
    Title of the document
  
  
    
    
    
    
  
2

_______13____105____15____12



  
    Title of the document
  
  
    
    
    
    
  
12



  
    Title of the document
  
  
    
    
    
    
  
3_______10


  
    Title of the document
  
  
    
    
    
    
  
15


  
    Title of the document
  
  
    
    
    
    
  
16


  
    Title of the document
  
  
    
    
    
    
  
17


  
    Title of the document
  
  
    
    
    
    
  
18


  
    Title of the document
  
  
    
    
    
    
  
19


  
    Title of the document
  
  
    
    
    
    
  
17


  
    Title of the document
  
  
    
    
    
    
  
21


  
    Title of the document
  
  
    
    
    
    
  
2



  
    Title of the document
  
  
    
    
    
    
  
23



  
    Title of the document
  
  
    
    
    
    
  
7__


  
    Title of the document
  
  
    
    
    
    
  
0


  
    Title of the document
  
  
    
    
    
    
  
26


  
    Title of the document
  
  
    
    
    
    
  
16


  
    Title of the document
  
  
    
    
    
    
  
17


  
    Title of the document
  
  
    
    
    
    
  
29


  
    Title of the document
  
  
    
    
    
    
  
2



  
    Title of the document
  
  
    
    
    
    
  
02


  
    Title of the document
  
  
    
    
    
    
  
32



  
    Title of the document
  
  
    
    
    
    
  
7


  
    Title of the document
  
  
    
    
    
    
  
05


  
    Title of the document
  
  
    
    
    
    
  
26


  
    Title of the document
  
  
    
    
    
    
  
2



  
    Title of the document
  
  
    
    
    
    
  
23



  
    Title of the document
  
  
    
    
    
    
  
7__


  
    Title of the document
  
  
    
    
    
    
  
0


  
    Title of the document
  
  
    
    
    
    
  
00


  
    Title of the document
  
  
    
    
    
    
  
19


  
    Title of the document
  
  
    
    
    
    
  
17


  
    Title of the document
  
  
    
    
    
    
  
03


  
    Title of the document
  
  
    
    
    
    
  
04


  
    Title of the document
  
  
    
    
    
    
  
17


  
    Title of the document
  
  
    
    
    
    
  
03


  
    Title of the document
  
  
    
    
    
    
  
07


  
    Title of the document
  
  
    
    
    
    
  
17


  
    Title of the document
  
  
    
    
    
    
  
09



  
    Title of the document
  
  
    
    
    
    
  
50


  
    Title of the document
  
  
    
    
    
    
  
51


  
    Title of the document
  
  
    
    
    
    
  
17


  
    Title of the document
  
  
    
    
    
    
  
53


  
    Title of the document
  
  
    
    
    
    
  
54



  
    Title of the document
  
  
    
    
    
    
  
55



  
    Title of the document
  
  
    
    
    
    
  
7__


  
    Title of the document
  
  
    
    
    
    
  
0


  
    Title of the document
  
  
    
    
    
    
  
58


  
    Title of the document
  
  
    
    
    
    
  
59


  
    Title of the document
  
  
    
    
    
    
  
58


  
    Title of the document
  
  
    
    
    
    
  
21



  
    Title of the document
  
  
    
    
    
    
  
23



  
    Title of the document
  
  
    
    
    
    
  
7__


  
    Title of the document
  
  
    
    
    
    
  
0


  
    Title of the document
  
  
    
    
    
    
  
25


  
    Title of the document
  
  
    
    
    
    
  
26


  
    Title of the document
  
  
    
    
    
    
  
17


  
    Title of the document
  
  
    
    
    
    
  
28


  
    Title of the document
  
  
    
    
    
    
  
2



  
    Title of the document
  
  
    
    
    
    
  
02


  
    Title of the document
  
  
    
    
    
    
  
71



  
    Title of the document
  
  
    
    
    
    
  
7____105____125____121



  
    Title of the document
  
  
    
    
    
    
  
23



  
    Title of the document
  
  
    
    
    
    
  
7_______10


  
    Title of the document
  
  
    
    
    
    
  
79


  
    Title of the document
  
  
    
    
    
    
  
19


  
    Title of the document
  
  
    
    
    
    
  
17


  
    Title of the document
  
  
    
    
    
    
  
02


  
    Title of the document
  
  
    
    
    
    
  
16


  
    Title of the document
  
  
    
    
    
    
  
17


  
    Title of the document
  
  
    
    
    
    
  
05


  
    Title of the document
  
  
    
    
    
    
  
06


  
    Title of the document
  
  
    
    
    
    
  
79


  
    Title of the document
  
  
    
    
    
    
  
21

Để bỏ chọn nút radio, bạn có thể sử dụng tập lệnh jQuery hoặc JavaScript. Hãy xem cách mỗi người trong số họ làm việc

Nếu bạn muốn sử dụng jQuery, thì lựa chọn đúng đắn để bỏ chọn nút radio một cách linh hoạt (chẳng hạn như khi nhấp vào nút) sẽ là sử dụng phương thức prop()

Hãy xem nó hoạt động như thế nào



  
    Title of the Document
    
  
  
    

Radio Button 1 Radio Button 2

Checked Radio Button 1 Checked Radio Button 2

Đây là giải pháp JavaScript để bỏ chọn nút radio



  
    Title of the document
  
  
    
    
    
    
  

Phương thức prop() được sử dụng để kiểm tra hoặc bỏ chọn hộp kiểm, chẳng hạn như khi nhấp vào nút. Phương thức yêu cầu jQuery 1. 6+ phiên bản. Phương thức prop() có lợi thế hơn so với phương thức. phương thức attr() khi cài đặt bị vô hiệu hóa và được kiểm tra. Phương thức thao tác với thuộc tính đã chọn và đặt nó thành đúng hoặc sai dựa trên việc bạn muốn kiểm tra hay bỏ chọn nó

Để đặt nút radio thành chọn/bỏ chọn, hãy chọn phần tử và đặt thuộc tính



  
    Title of the document
  
  
    
    
    
    
  
08 của nó thành


  
    Title of the document
  
  
    
    
    
    
  
09 hoặc


  
    Title of the document
  
  
    
    
    
    
  
10, e. g.


  
    Title of the document
  
  
    
    
    
    
  
11. Khi được đặt thành


  
    Title of the document
  
  
    
    
    
    
  
09, nút radio trở thành


  
    Title of the document
  
  
    
    
    
    
  
08 và tất cả các nút radio khác có cùng thuộc tính


  
    Title of the document
  
  
    
    
    
    
  
14 sẽ không được chọn

Đây là HTML cho các ví dụ trong bài viết này



  
    
  

  
    
    Yes

    
    No

    
    Maybe

    
  

Và đây là mã JavaScript liên quan

Làm cách nào để bỏ chọn nút radio đã chọn trong JavaScript?

Chúng tôi đã chọn nút radio bằng phương pháp



  
    Title of the document
  
  
    
    
    
    
  
15

Sau đó, chúng tôi đặt thuộc tính được kiểm tra của phần tử thành



  
    Title of the document
  
  
    
    
    
    
  
09

Nếu bạn cần bỏ chọn một nút radio cụ thể, hãy đặt thuộc tính đã chọn của nó thành



  
    Title of the document
  
  
    
    
    
    
  
10

Lưu ý rằng việc kiểm tra một nút radio, sẽ tự động bỏ chọn tất cả các nút radio khác có cùng thuộc tính



  
    Title of the document
  
  
    
    
    
    
  
14

const yesBtn = document.getElementById('yes');
yesBtn.checked = true;

const noBtn = document.getElementById('no');
noBtn.checked = true;

Đầu tiên chúng tôi kiểm tra nút radio

const yesBtn = document.getElementById('yes');
yesBtn.checked = true;

const noBtn = document.getElementById('no');
noBtn.checked = true;
3, nhưng khi chúng tôi kiểm tra nút
const yesBtn = document.getElementById('yes');
yesBtn.checked = true;

const noBtn = document.getElementById('no');
noBtn.checked = true;
4, nút
const yesBtn = document.getElementById('yes');
yesBtn.checked = true;

const noBtn = document.getElementById('no');
noBtn.checked = true;
3 tự động được bỏ chọn

Bạn có thể đặt nút radio được kiểm tra theo mặc định bằng cách đặt thuộc tính



  
    Title of the document
  
  
    
    
    
    
  
08 trực tiếp trên trường
const yesBtn = document.getElementById('yes');
yesBtn.checked = true;

const noBtn = document.getElementById('no');
noBtn.checked = true;
7

Lưu ý rằng nếu bạn không gán cùng một giá trị thuộc tính



  
    Title of the document
  
  
    
    
    
    
  
14 cho tất cả các trường
const yesBtn = document.getElementById('yes');
yesBtn.checked = true;

const noBtn = document.getElementById('no');
noBtn.checked = true;
7 radio trong nhóm, bạn sẽ có thể kiểm tra nhiều nút radio cùng một lúc

Nếu đây là hành vi mà trường hợp sử dụng của bạn yêu cầu, bạn nên sử dụng hộp kiểm thay vì nút radio

Bạn có thể xem các ví dụ trực tuyến sử dụng phương pháp



  
    Title of the document
  
  
    
    
    
    
  
080 và


  
    Title of the document
  
  
    
    
    
    
  
081 để kiểm tra và bỏ chọn nút radio

Điều này cũng hoàn toàn ổn, tuy nhiên có một số điều cần lưu ý khi sử dụng phương pháp



  
    Title of the document
  
  
    
    
    
    
  
080

Phương thức nhận 2 tham số sau

  1. 
    
      
        Title of the document
      
      
        
        
        
        
      
    
    14 - tên của thuộc tính được đặt
  2. 
    
      
        Title of the document
      
      
        
        
        
        
      
    
    084- giá trị để gán cho thuộc tính

Nếu thuộc tính đã tồn tại trên phần tử, giá trị sẽ được cập nhật, nếu không, một thuộc tính mới được thêm vào với tên và giá trị được chỉ định

Khi đặt giá trị cho thuộc tính boolean, chẳng hạn như



  
    Title of the document
  
  
    
    
    
    
  
08, chúng ta có thể chỉ định bất kỳ giá trị nào cho thuộc tính và nó sẽ hoạt động

Nếu thuộc tính có mặt, bất kể giá trị là gì, thì giá trị của nó được coi là



  
    Title of the document
  
  
    
    
    
    
  
09

Nếu không có thuộc tính boolean, chẳng hạn như



  
    Title of the document
  
  
    
    
    
    
  
08, thì giá trị của thuộc tính được coi là


  
    Title of the document
  
  
    
    
    
    
  
10

Khi đặt thuộc tính



  
    Title of the document
  
  
    
    
    
    
  
08, chúng tôi chuyển một chuỗi trống làm giá trị cho thuộc tính vì cách tốt nhất là đặt thuộc tính boolean không có giá trị

Làm cách nào để đặt lại nút radio trong JavaScript?

Làm cách nào để bỏ chọn đài trong JavaScript? .
tài liệu. getElementById('gửi'). onclick = chức năng () {
đài phát thanh var = tài liệu. querySelector('input[type=radio][tên=ngôn ngữ]. đã kiểm tra');
Đài. đã kiểm tra = sai;

Làm cách nào để bỏ nhấp vào nút radio trong HTML?

Trong HTML, nút radio được sử dụng để chọn một trong nhiều lựa chọn đã cho. Các nút radio được hiển thị trong các nhóm radio để hiển thị một tập hợp các tùy chọn có liên quan, chỉ một trong số đó có thể được chọn. Một nút radio trong HTML có thể được xác định bằng cách sử dụng thẻ .