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
  • 345
  • 1
  • 2
  • 9

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,879
  • 9
  • 49
  • 70
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,901
  • 5
  • 35
  • 57
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
  • 74,255
  • 12
  • 74
  • 103
Van
  • 600
  • 4
  • 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
  • 387
  • 5
  • 17