Replacing If/Else in Javascript

Asked

Viewed 26 times

0

I am creating a form that will have several checkbox:

<div class="checkbox">
  <label>
        <input name="mini" id="SistemaRestauracao" class="mini" type="checkbox" value="1">
        <b> Sistemas de Restauração - </b> 8h às 12h e 14h às 18h
      </label>
</div>
<div class="checkbox">
  <label>
        <input name="mini" id="custoRodoviarios" class="mini" type="checkbox" value="2">
        <b> Custos Rodoviários - </b> 8h às 12h e 14h às 18h
      </label>
</div>
<div class="checkbox">
  <label>
        <input name="mini" id="questoesAmbientais" class="mini" type="checkbox" value="3">
        <b> Questões ambientais - </b> 8h às 12h
      </label>
</div>
<div class="checkbox">
  <label>
        <input name="mini" id="sinalizacaoSeguranca" class="mini" type="checkbox" value="4">
        <b> Sinalização e Segurança - </b> 14h às 18h
      </label>
</div>
<div class="checkbox">
  <label>
        <input name="mini" id="sustentabilidade" class="mini" type="checkbox" value="5">
        <b> Sustentabilidade portuária - </b> 8h às 12h
      </label>
</div>
<div class="checkbox">
  <label>
        <input name="mini" id="economia" class="mini" type="checkbox" value="6">
        <b> Economia de transporte - </b> 14h às 18h
      </label>
</div>
<br>
<label>Visita Técnica</label>
<div class="checkbox">
  <label>
        <input type="checkbox" id="visitaManha" value="">
        <b> TRANSPORTE FERROVIARIO E MARÍTIMO - </b> 9h às 12h
      </label>
</div>
<div class="checkbox">
  <label>
        <input type="checkbox" id="visitaTarde" value="">
        <b> TRANSPORTE FERROVIARIO E MARÍTIMO - </b>14h às 17h
      </label>
</div>

I need to create a validation system where a checkbox should be disabled when the other of the same time is selected. Ex. I selected a course from 08:00 to 12:00 so disable the full day courses and the others from 08:00 to 12:00 and so on...

I can do this using if/Else condition, but it is giant with both "if". What other Javascript/Jquery solution could I use to avoid using so many ifs?

  • The easiest way is to represent these properties as objects. You collect all of these objects in a vector, then when you select something in the checkbox, you would pass to a value-changing detector the related objects and then go through the vector of checkboxes that are tied to the other courses, unchecking those whose objects collide schedules. This means possibly changing how you populate your html

  • Okay, maybe it’s not easier, but it provides more semantics and syntactic structure to work with; I think it’s always better to work in a full semantic environment, so it would be easier for me

  • 4

    I don’t know, can’t work with radio button?

No answers

Browser other questions tagged

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