firing a jQuery change function at document ready

后端 未结 4 1559
醉酒成梦
醉酒成梦 2021-02-02 12:01

My change function allows users to switch from country to country and get different text and features. It works when changing country selections. But at initial page load, it do

4条回答
  •  野的像风
    2021-02-02 12:37

    Just chain .trigger('change') to the end of the handler assignment.

     // ----------v-------v-----quotation marks are mandatory
    $('input[name="country"]').change(function ()
    {                                                                               
    // change user instructions to be country specific
        switch ($('input[name="country"]:checked').val())
        {
            case 'US':
            $('#stateMessage1').text('2. Select a state or territory of the United States.');
            $('#stateMessage2').text('Start typing a state of the United States, then click on it in the dropdown box.');
            $('#threeSelects').show();
            $('#twoSelects').hide();
            //select via 3 steps                        
            break;           
            case 'CA':
            $('#stateMessage1').text('2. Select a province or territory of Canada.');
            $('#stateMessage2').text('Start typing a province of Canada, then click on it in the dropdown box.');
            $('#twoSelects').show();
            $('#threeSelects').hide();
            //select town via 2 steps - country, town           
            break;         
        }
    }).trigger('change');  // <--- RIGHT HERE
    

    Or if you only want it to fire on the first element, use triggerHandler() instead.

            // ...
            $('#twoSelects').show();
            $('#threeSelects').hide();
            //select town via 2 steps - country, town           
            break;         
        }
    }).triggerHandler('change');  // <--- RIGHT HERE
    

提交回复
热议问题