Toggle Yes / NO with multiple questions

后端 未结 3 540
太阳男子
太阳男子 2020-12-20 03:49

I have 5 questions in a form, and a few of them have a YES / NO response. I wanted to just be able to toggle the yes and no buttons once the user selected one of them. The i

3条回答
  •  时光取名叫无心
    2020-12-20 04:43

    You need to add a unique id or class for each button and link a click function for each button like this:

    $('.btn-toggle').click(function() {
        $(this).toggleClass('active'); 
            console.log($(this));
     });
    
    $('.btn-toggle2').click(function() {
        $(this).toggleClass('active'); 
            console.log($(this));
     });
    
    $('.btn-toggle3').click(function() {
        $(this).toggleClass('active'); 
            console.log($(this));
     });
    
    $('.btn-toggle4').click(function() {
        $(this).toggleClass('active'); 
            console.log($(this));
     });
    

    Or you can group them together like this if they have a common function:

    $('.btn-toggle', '.btn-toggle2', '.btn-toggle3', '.btn-toggle4') .click(function() {
        $(this).toggleClass('active'); 
            console.log($(this));
     });
    

提交回复
热议问题