2
$('.sale_type').click(function() {}

I have such a function,if I click the radio,the function will execute,but how to trigger the click if the radio is checked when the page is loaded.

makenai
  • 175
  • 1
  • 8

4 Answers4

2

You can use jquery .trigger() when dom is ready:

$(":button").on("click", function() {
  alert("Ok");
});
//here check if radio is checked
if ($(":radio").prop("checked"))
  $(":button").trigger("click");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" checked />
<input type="button" value="test" />

References

.prop()

Alex Char
  • 32,295
  • 8
  • 49
  • 69
2

Consider moving the code that gets triggered when the radio is clicked in to its own function. That way, you don't need to simulate/trigger events to execute the function:

$('.sale_type').on('click', function() {
    doStuff();
});

$(document).on('ready', function() {
    if($('.sale_type').is(':checked')) {
        doStuff();
    }
});

function doStuff() {
   console.log('function code will be here');
}
Andy
  • 4,655
  • 5
  • 33
  • 56
0

use:

$('.sale_type').trigger("click")

Just remember you selected by class so all element with this class. Will have their onclick function (if any) triggerd.

Jai
  • 72,925
  • 12
  • 73
  • 99
Van
  • 590
  • 3
  • 12
0

You can use this way

  • Check your element when page load

    $(document).read(function(){

        var checkElementChecked = $('.sale_type').is(":checked");       
    
        if(checkElementChecked === true)
        {
            $('.sale_type').trigger('click');
        }
    });
    
Binh LE
  • 377
  • 5
  • 17