Select(HTML) with multiple selection. Mark one and should mark all, not working

Asked

Viewed 73 times

2

I have this select with multiples select

<tr>
                                <td width="10%" class="label_right">Autorização Prévia: &nbsp;</td>
                                <td class="label_left">
                                    <select id="ddl_autorizacaoprevia" multiple>
                                        <option value="0">TODAS AS AUTORIZAÇÕES</option>
                                        <option value="T">TÉCNICA / ADMINISTRATIVA</option>
                                        <option value="A">SISTÊMICA</option>
                                        <option value="N">NÃO PRECISA</option>
                                    </select>
                                </td>
                            </tr>

What I wish is that when I select "ALL AUTHORISATIONS", all others should be selected and when I click on any other, all the selected ones should be unchecked and only the selected one should be checked (selected). I really don’t know how to do this. I’m looking on the internet, but so far I haven’t found anything.

1 answer

1


With jquery it would be something like this:

$(document).ready(function(){

   $('#ddl_autorizacaoprevia option').click(function(){
       var that = $(this);
       if ($(that).val() == 0){
          $('#ddl_autorizacaoprevia option').each(function(){
             $(this).attr('selected', 'selected');
          });
       }  else  {
          $('#ddl_autorizacaoprevia option').each(function(){
             $(this).removeAttr('selected'); 
          });
          $(that).attr('selected', 'selected');
       }

   }

});

Something on that line should solve your problem

  • Thiesen, what is optSelected?

  • I switched optSelected by option. Selected, but it gives this error: Uncaught ReferenceError: option is not defined. I need some include?

  • I edited there, actually Voce has to catch the guy who received the click

  • Yes, I did that and mark them all, but I can’t just mark one. I select, for example, and when I click on an option, it’s not checked.

  • 1

    I resolved. that.attr('selected', 'selected'); instead of $(that).attr('selected');

  • There is a problem. It does not work in IE and the browser that has to work is IE. Chrome works well.

  • Jquery is cross browser, it must be an old version of IE...

  • Version 11.0.24. It’s new my IE. It won’t even go with angry prayer. With F12, it gives an error of: Unspecified error.

  • IE is always a problem friend, no matter the kkk version

  • This is a fucking disgrace.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.