Load page with Jquery event

Asked

Viewed 835 times

3

need a help. There’s a jQuery event that works the way I want it to. When I click, it executes the action.

   $("#SEN").click(function(){
   $("#abre").show(); //ou fadeIn
   });

My intention is that, on another page, retrieves data from a customer and when I leave checked in the radio that performs the action, not only takes by loading the page.

Summary is to load the page and run the radio selected without user interaction.

I’ve tried that and it won’t:

 $("#SEN").bind('onload, change',function(){
 $("#abre").show();
 });

so tbm:

 $('#SEN').on('onload, change',function() {
  if (this.checked == true) {
    $("#abre").show();
  } 
});

And with Trigger and nothing :(

 $('#SEN').on('onload, change',function() {
  if (this.checked == true) {
    $("#abre").show();
  } 
}).trigger("change");

Maybe you’re doing it wrong You can help me?

4 answers

2

It was a little confusing your explanation, but it would be when the radio is checked perform an action?

can be done this way:

if ($("#mycheckbox").prop("checked")) {
  alert("Execute uma ação");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="radio" name="mycheckbox" id="mycheckbox" checked/>

  • It would be on page loading the already selected event run

  • What is the difficulty? and only put to check if it is checked when loading the page.

  • worked like this $('#SEN'). on('onload, change',Function() { if (this.checked == true) { $("#opens"). show(); } }). Trigger("change");

0

I think you need something that way:

$('.checks')
    .on('load, change',function() {
       var i=0;
     $("input.checks:checked").each(function(){
         i++;
      });

       if ( i > 0) {
           $('#abre').show();
       } else {
           $('#abre').hide();
       }

}).trigger("change");

And the HTML:

<input type="checkbox" class="checks"> Elemento checado 1<br>
<input type="checkbox" class="checks"> Elemento checado 2<br>
<input type="checkbox" class="checks"> Elemento checado 3
<div id="abre" style="display:none">olá!</div>

Here the example: http://jsfiddle.net/ivanferrer/5n7dyvx7/

0


I got it this way:

 $('#SEN').on('onload, change',function() {
  if (this.checked == true) {
    $("#abre").show();
  } 
}).trigger("change");
  • could also use if ($('#elemento').is(':checked')) { $("#abre").show(); }

  • Your explanation became incomprehensible, I just understood what you wanted by the code you tried to do.

  • I’ll explain again. There’s an event on the page that works perfectly with click. right? In that same screen I recover data from clients and wanted that when loading the page as it would already be selected the select radio etc, open the event in the page load. Got it now?

0

I believe your code problem is the event you are passing as parameter.

According to the jquery documentation, when you use the "on" method, the event does not have "on" in the name, for example, if the javascript event is "onload", you should pass as parameter only "load".

https://api.jquery.com/on/#on-Events-selector-data-Handler

Also, when handling the "change" event, probably the "checked" is from another element that will not be "this" because this is a "select" element and not a "checkbox", so you have to take the checkbox by id or in some other way.

Browser other questions tagged

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