我的更改功能允许用户从国家切换到国家并获得不同的文本和功能。它在更改国家/地区选择时有效。但在初始页面加载时,它不会触发jQuery更改来为默认/初始国家设置隐藏和显示文本div。在文档准备就绪时触发jQuery更改功能
这两个div显示在初始页面加载。当我离开,然后回到默认/初始国家时,改变火势,隐藏并显示火情,并显示正确的信息和功能。
我已经尝试过document.ready
,它们在开关选择内部和更改功能外都具有更改功能。既不工作 - 也不会在doc开头的情况下在开关盒中触发隐藏,显示和其他jQuery。我不清楚'准备'是否是一个有效的触发事件。
我也曾尝试:
$('input[name=country]').value('US').trigger('click');
,但它打破了变化的功能。这是代码。下面的国家选择只是简单的两个国家,但实际上有很多。
$(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;
}
});
});
这起作用。谢谢。它循环25次。是否有另一种方式来做到这一点,不循环 - 或我是挑剔;-)我确实希望它启动整个更改功能,因为许多国家中的任何一个都可能是最初的条件 - 所以triggerHandler首先不是它。 – 2011-05-17 01:09:32
@Mike_Laird:如果你说你只是想让它触发选中的,那就改为:'.filter(':checked')。trigger('change');' – user113716 2011-05-17 01:38:45
你钉了它!真棒。在加载时只发生一次火灾,之后每次发生一次火灾。显示,隐藏等正确地做他们的东西。非常感谢。 – 2011-05-17 01:57:52