我的更改功能允许用户从一个国家切换到另一个国家并获得不同的文本和功能。它在更改国家/地区选择时起作用。但在初始页面加载时,它不会触发 jQuery 更改来设置默认/初始国家/地区的隐藏和显示文本 div。
两个 div 都会在初始页面加载时显示。当我离开然后返回默认/初始国家/地区时,更改火力,隐藏和显示火力,并显示正确的信息和功能。
我努力了document.ready
在开关选择内部和更改功能外部都具有更改功能。两者都不起作用 - 它们不会在文档准备好时触发开关案例中的隐藏、显示和其他 jQuery。我不清楚“准备好”是否是有效的触发事件。
我也尝试过:
$('input[name=country]').value('US').trigger('click');
但它破坏了更改功能。这是代码。为了简单起见,下面选择的国家/地区仅是 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;
}
});
});
刚链.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
或者,如果您只想让它在第一个元素上触发,请使用triggerHandler()
反而。
// ...
$('#twoSelects').show();
$('#threeSelects').hide();
//select town via 2 steps - country, town
break;
}
}).triggerHandler('change'); // <--- RIGHT HERE
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)