conditional Javascript

1

How can I do the following in a conditional?

                 $(function () {

                     $("[id*='_btnCheck']").click(function () {



                         var buttonName = $(this).attr('id');
                         ChannelEvent(buttonName);
                     });

                     $("[id*='_btnRuta']").click(function () {
                         var buttonRuta = $(this).attr('id');
                         RouteEvent(buttonRuta);
                     });



                 });

If I click on any button that carries in the id _btnCheck then something happens, if I click on a button that takes in the id _btnRuta then something happens, and finally if I click on a button that carries id _btnCliente then something happens.

My code is functional, but I have been left with this small limitation.

Update:

I'll explain myself better let's say I have 10 buttons and these are from the group A , another 10 buttons are from group B , other 10 group C .

Each button has a different ID. The code that I have put is to track a key within the id button. If we are in the group A . all buttons carry the key: _btnCheck , but obvious the full ID of a button in group A can be: e1_btnCheck , Other is e2_btnCheck , Other is e3_btnCheck and so on.

So when I click on them, they take me to the same method thanks to the key that contains a part of the ID.

Well, now, there are many buttons but these are grouped thanks to that key.

When I use:

        $(function () {

                     $("[id*='_btnCheck']").click(function () {



                         var buttonName = $(this).attr('id');
                         ChannelEvent(buttonName);
                     });
  });

The button responds with I wish and guides me to the event, which then becomes a webmethod on ASP.net.

If I put the code to find the following groups, B and C . The click event does not fire. Only for group A only. So I was thinking about doing a conditional which may work.

If you have a better idea, welcome it. I'm not very good at JS. Thanks.

    
asked by N'oel C'alero 13.05.2016 в 00:11
source

3 answers

2

So you'd better use classes to differentiate the buttons:

$('.btn').click(function(){
  var $boton = $(this);
  if ($boton.hasClass('btnCheck')){
    ChannelEvent($boton.attr('id'));
  }
  if ($boton.hasClass('btnRuta')){
    RouteEvent($boton.attr('id'));
  }
  if ($boton.hasClass('btnCliente')){
    alert('Botón cliente ' + $boton.attr('id'));
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn btnCheck" id="boton1">Check 1</Button>
<button class="btn btnCheck" id="boton2">Check 2</Button>
<button class="btn btnCheck" id="boton3">Check 3</Button>
<button class="btn btnCheck" id="boton4">Check 4</Button>
<button class="btn btnCheck" id="boton5">Check 5</Button>
<button class="btn btnRuta" id="boton6">Ruta 1</Button>
<button class="btn btnRuta" id="boton7">Ruta 2</Button>
<button class="btn btnRuta" id="boton8">Ruta 3</Button>
<button class="btn btnRuta" id="boton9">Ruta 4</Button>
<button class="btn btnRuta" id="boton10">Ruta 5</Button>
<button class="btn btnCliente" id="boton11">Cliente 1</Button>
<button class="btn btnCliente" id="boton12">Cliente 2</Button>
<button class="btn btnCliente" id="boton13">Cliente 3</Button>
<button class="btn btnCliente" id="boton14">Cliente 4</Button>
<button class="btn btnCliente" id="boton15">Cliente 5</Button>
    
answered by 13.05.2016 / 00:42
source
1

This could help you, you just have to add a common class to all your buttons in this case btn , for example:

    
    $(".btn").on("click",function(){
      
      switch($(this).attr('id')){
          case "_btnCheck":{
              //tu codigo para este caso
              alert("Click en check");
              break;
          }
          case "_btnRuta":{
              //tu codigo para este caso
              alert("Click en ruta");
              break;
          }
          case "_btnCliente":{
              //tu codigo para este caso
              alert("Click en cliente");
              break;
          }
      }
      
    });

//Este codigo es para resolver la duda en el comentario:

$(".grupoa").on("click", function () {
ChannelEventA($(this).attr('id'));
});

$(".grupob").on("click", function () {
ChannelEventB($(this).attr('id'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn" id="_btnCheck">Check</Button>
<button class="btn" id="_btnRuta">Ruta</Button>
<button class="btn" id="_btnCliente">Cliente</Button>
    
answered by 13.05.2016 в 00:23
0

This is an option:

$('button[type=submit]').click(function() {
 switch($(this).attr('id')){
 case "_btnCheck":{
    alert("el boton seleccionado fue: " + $(this).text());
   break;
 }
 case "_btnRuta":{
  alert("el boton seleccionado fue: " + $(this).text());
  break;
 }
 case "_btnCliente":{
   alert("el boton seleccionado fue: " + $(this).text());
  break;
 }
 } 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="submit" id="_btnCheck" value="Accept">Check</button>
<button type="submit" id="_btnRuta" value="Decline">Ruta</button>
<button type="submit" id="_btnCliente" value="Decline">Cliente</button>
    
answered by 13.05.2016 в 00:20