2017-10-06 1 views
0

나는 PHP와 스크립트를 처음 사용합니다. 작은 일을 해결하도록 도와주세요.스크립트로 드롭 다운의 자리 표시 자

나는 우리가 어떤 국가를 선택할 때까지 http://jsfiddle.net/bdhacker/eRv2W/

내가 그 빈을 국가 드롭 다운 목록을 국가가 이미 "국가 선택"을 표시 위치를 명시하지만, 주/지방에 ... 여기에서 내 코드를 얻을. Select Country와 같은 Select State/Province를 사전에 표시하고 싶습니다.

여기 여기 내 스크립트가

// Countries 
 
var country_arr = new Array("Canada", "United States"); 
 

 
// States 
 
var s_a = new Array(); 
 
s_a[0] = ""; 
 
s_a[1] = "Alberta|British Columbia|Manitoba|New 
 
Brunswick|Newfoundland|Northwest Territories|Nova 
 
Scotia|Nunavut|Ontario|Prince Edward Island|Quebec|Saskatchewan|Yukon 
 
Territory"; 
 
s_a[2] = "Alabama|Alaska|Arizona|Arkansas|California|Colorado|Connecticut|Delaware| 
 
District of Columbia|Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa| 
 
Kansas| Kentucky|Louisiana|Maine|Maryland|Massachusetts|Michigan| 
 
Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New Hampshire 
 
|New Jersey|New Mexico|New York|North Carolina|North 
 
Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|Rhode Island| 
 
South Carolina|South Dakota|Tennessee|Texas|Utah|Vermont|Virginia 
 
|Washington|West Virginia|Wisconsin|Wyoming"; 
 

 
function populateStates(countryElementId, stateElementId) { 
 

 
var selectedCountryIndex = document.getElementById(countryElementId).selectedIndex; 
 
var stateElement = document.getElementById(stateElementId); 
 

 
stateElement.length = 0; // Fixed by Julian Woods 
 
stateElement.options[0] = new Option('Select Province/State', ''); 
 
stateElement.selectedIndex = 0; 
 

 
var state_arr = s_a[selectedCountryIndex].split("|"); 
 

 
for (var i = 0; i < state_arr.length; i++) { 
 
stateElement.options[stateElement.length] = new Option(state_arr[i], 
 
state_arr[i]); 
 
    } 
 
} 
 

 
function populateCountries(countryElementId, stateElementId) { 
 
// given the id of the <select> tag as function argument, it inserts 
 
<option> tags 
 
var countryElement = document.getElementById(countryElementId); 
 
countryElement.length = 0; 
 
countryElement.options[0] = new Option('Select Country', '-1'); 
 
countryElement.selectedIndex = 0; 
 
for (var i = 0; i < country_arr.length; i++) { 
 
    countryElement.options[countryElement.length] = new 
 
Option(country_arr[i], country_arr[i]); 
 
} 
 

 
// Assigned all countries. Now assign event listener for the states. 
 

 
if (stateElementId) { 
 
    countryElement.onchange = function() { 
 
     populateStates(countryElementId, stateElementId); 
 
     }; 
 
    } 
 
} 
 

 

 
    <!-- HTML --> 
 
    <select id="country" name="country"></select> 
 
    <br /> 
 
    
 
    <select name="state" id="state"></select> 
 
    <br/> 
 
    
 
    <script language="javascript"> 
 
     populateCountries("country", "state"); 
 
     
 
    </script>

가 사전에 너무 감사 나의 일 연결 https://www.paradoxaccess.com/lp/country/Access_Roads.html

입니다.

+0

이 답변을 살펴 보셨습니까? https://stackoverflow.com/questions/5805059/how-do-i-make-a-placeholder-for-a-select-box – NewToJS

답변

0

populateStates 함수를 onchange 함수 외부에서 호출하여 기본 항목을 실행하고 채우도록하십시오.

// Assigned all countries. Now assign event listener for the states. 
if (stateElementId) { 
    populateStates(countryElementId, stateElementId); 
    countryElement.onchange = function() { 
     populateStates(countryElementId, stateElementId); 
    }; 
}