在文档就绪时触发jQuery更改函数

Mik*_*ird 19 jquery initialization onchange

我的更改功能允许用户从一个国家切换到另一个国家,并获得不同的文本和功能.它在更改国家/地区选择时有效.但是在初始页面加载时,它不会触发jQuery更改来设置隐藏和显示默认/初始国家/地区的文本div.

两个div都显示在初始页面加载时.当我改变然后回到默认/初始国家时,更改火灾,隐藏和显示火灾,并显示正确的信息和功能.

我已尝试document.ready在开关选择内部和更改功能外部使用更改功能.两者都没有工作 - 他们不会在doc准备好的交换机案例中激活hide,show和其他jQuery.我不清楚'ready'是否是一个有效的触发事件.

我也尝试过:

$('input[name=country]').value('US').trigger('click'); 
Run Code Online (Sandbox Code Playgroud)

但它打破了改变功能.这是代码.下面的国家选择只是2个国家,以保持简单,但实际上,有很多.

$(document).ready(function()
{
//1st tier - select country via radio buttons:           
//Initialize country
$('input[name=country]:first').attr('checked', true);   //Set first radio button (United States)
//$('input[name=country]:first').attr('checked', true).trigger('ready');  //sets 1st button, but does not fire change
//$('input[name=country]:first').trigger('click'); //sets 1st button, but does not fire change

$('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;         
    }
});
});
Run Code Online (Sandbox Code Playgroud)

use*_*716 33

只链.trigger('change')到处理程序赋值的末尾.

 // ----------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
Run Code Online (Sandbox Code Playgroud)

或者,如果您只想在第一个元素上触发它,请triggerHandler()改用.

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

  • @Mike_Laird:如果你说你只想要它来触发被检查的那个,那就改为:`.filter(':checked').trigger('change');` (3认同)