简体   繁体   中英

On load on a select option

I have a select option that when it changes it activates a script. What I'm trying to do is at the start when there is a selected value in the select option It would also activate the script. Right now here is my code

I know that onload does not work on select option. So could you give me an idea for an alternative? Here is my code

<script>

function getState(val) {

    var group = $("#group").val();
        $.ajax({
            type: "GET",
            url: "<?=base_url()?>personnel/get_division/"+val,
            data:{"group": group},
            success: function(data){
                $('#div1').html(data);
             }
        }); 
}
</script>

<select id="grp" name="group" class="cust_fields" onChange="getState(this.value);">

                    <option value=""> -- Select -- </option> 

                    <?php $table = "tbl_group"; $select = "g_name, g_id"; ?>

                    <?php foreach($ddl1 as $row1){ ?> 

                    <option value="<?=$row1['g_id']?>"> <?=$row1['g_name']?> </option> 

                    <?php } ?>

                </select>

Since you have an onChange event tied to each element, you could trigger the event on the selected item on page load.

jQuery(document).ready(function($) {

    //$('option:selected').trigger('change');
    $('select').trigger('change');

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM