2011-05-16 110 views
19

我的更改功能允許用戶從國家切換到國家並獲得不同的文本和功能。它在更改國家/地區選擇時有效。但在初始頁面加載時,它不會觸發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;   
    } 
}); 
}); 

回答

32

只要將.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 
+0

這起作用。謝謝。它循環25次。是否有另一種方式來做到這一點,不循環 - 或我是挑剔;-)我確實希望它啓動整個更改功能,因爲許多國家中的任何一個都可能是最初的條件 - 所以triggerHandler首先不是它。 – 2011-05-17 01:09:32

+3

@Mike_Laird:如果你說你只是想讓它觸發選中的,那就改爲:'.filter(':checked')。trigger('change');' – user113716 2011-05-17 01:38:45

+0

你釘了它!真棒。在加載時只發生一次火災,之後每次發生一次火災。顯示,隱藏等正確地做他們的東西。非常感謝。 – 2011-05-17 01:57:52

2

爲什麼在選擇合適的單選按鈕之後不要觸發change

$('input[name=country]').change(); 

這相當於

$('input[name=country]').trigger('change'); 
+0

這不火的變化功能。顯示和隱藏不執行。 – 2011-05-17 01:01:50

+0

它確實 - 頁面上的所有其他答案都做同樣的事情。必須有其他的東西導致它不能工作 - 也許你放置觸發器的地方。 – 2011-05-17 02:19:32

0

之前最終});,加入$( '輸入')的變化();

+0

這工作。謝謝。它在初始頁面加載時循環25次。還有另一種方法可以做到不循環 - 或者我是挑剔的;-)之後,對於一個國家的變化,它只執行一次。所以就行爲而言,它的行爲與上面的帕特里克dw建議相同。 – 2011-05-17 01:28:42

0

這通常是由如下:

function onChange() 
{                    
    switch ($('input[name=country]:checked').val()) 
    .... 
} 

$('input[name=country]').change(onChange); // 1. attach it as event handler. 
onChange();        // 2. call it first time. 
+1

你可能在這裏有東西,但我是新手,我無法讓你的建議工作。該頁面不會加載。說明太簡單。如果你想把這個建議擴展到更多的實現細節,我會再試一次。 – 2011-05-17 01:45:07