jQuery check/uncheck radio button onclick

前端 未结 24 2776
滥情空心
滥情空心 2020-12-01 05:23

I have this code to check/uncheck a radio button onclick.

I know it is not good for the UI, but I need this.

$(\'#radioinstant\').click(function() {          


        
24条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 05:41

    I was having a related problem - I had to open a dialog box from dropdown based on selection from dropdown I will showing one or two radio button. Only one radio button can be activated at a time.

    • Option 1 will show two radio button with 1st radio selected
    • Option 2 will show second radio button and selected.

    The script works some time some time it injects the checked but checkbox still unchecked

    I went to jQuery .prop(), seems like jquery .prop() .attr() has some complication with radio buttons while using attr or prop. So What I did instead is trigger the click on the radio button based on Select value.

    That resolve the need of using attr or prop or using on off lengthy code.

    myForm = $("#myForm");
    
    if (argument === 'multiple') {
                myForm.find('#radio1').parent('li').hide();
                myForm.find('#radio2').trigger('click');
        }
        else{
            myForm.find('#radio1').trigger('click');
             myForm.find('#radio1').parent('li').show();
        }
    

    Not sure if this is best practice but it - Works like charm

提交回复
热议问题