gpt4 book ai didi

javascript - 在 javascript 上运行的动态国家/地区选择。如何更改状态的默认选择?

转载 作者:行者123 更新时间:2023-11-28 06:19:55 27 4
gpt4 key购买 nike

美好的一天,该来源现在允许我预先选择默认国家/地区。但我还需要预选默认状态。不知道如何修改脚本。基本上我想将我的 PHP 代码绑定(bind)到选择框表单,并且每个帐户都能够预先选择国家/地区阶段。非常感谢您的帮助。

//////////////////这包含标题为country.js的JAVA脚本文件///////////////

// Setting Arrays ///////////////////////////////////////////////////////////

var country_arr = new Array( "USA", "Canada", "United Kingdom");




// States

var s_a = new Array();
s_a[0]="";
s_a[1]="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";
s_a[2]="Alberta|British Columbia|Manitoba|New Brunswick|Newfoundland|Northwest Territories|Nova Scotia|Nunavut|Ontario|Prince Edward Island|Quebec|Saskatchewan|Yukon Territory";
s_a[3]="Barking and Dagenham|Barnet|Barnsley|Bath and North East Somerset|Bedfordshire|Bexley|Birmingham|Blackburn with Darwen|Blackpool|Bolton|Bournemouth|Bracknell Forest|Bradford|Brent|Brighton and Hove|Bromley|Buckinghamshire|Bury|Calderdale|Cambridgeshire|Camden|Cheshire|City of Bristol|City of Kingston upon Hull|City of London|Cornwall|Coventry|Croydon|Cumbria|Darlington|Derby|Derbyshire|Devon|Doncaster|Dorset|Dudley|Durham|Ealing|East Riding of Yorkshire|East Sussex|Enfield|Essex|Gateshead|Gloucestershire|Greenwich|Hackney|Halton|Hammersmith and Fulham|Hampshire|Haringey|Harrow|Hartlepool|Havering|Herefordshire|Hertfordshire|Hillingdon|Hounslow|Isle of Wight|Islington|Kensington and Chelsea|Kent|Kingston upon Thames|Kirklees|Knowsley|Lambeth|Lancashire|Leeds|Leicester|Leicestershire|Lewisham|Lincolnshire|Liverpool|Luton|Manchester|Medway|Merton|Middlesbrough|Milton Keynes|Newcastle upon Tyne|Newham|Norfolk|North East Lincolnshire|North Lincolnshire|North Somerset|North Tyneside|North Yorkshire|Northamptonshire|Northumberland|Nottingham|Nottinghamshire|Oldham|Oxfordshire|Peterborough|Plymouth|Poole|Portsmouth|Reading|Redbridge|Redcar and Cleveland|Richmond upon Thames|Rochdale|Rotherham|Rutland|Salford|Sandwell|Sefton|Sheffield|Shropshire|Slough|Solihull|Somerset|South Gloucestershire|South Tyneside|Southampton|Southend-on-Sea|Southwark|St. Helens|Staffordshire|Stockport|Stockton-on-Tees|Stoke-on-Trent|Suffolk|Sunderland|Surrey|Sutton|Swindon|Tameside|Telford and Wrekin|Thurrock|Torbay|Tower Hamlets|Trafford|Wakefield|Walsall|Waltham Forest|Wandsworth|Warrington|Warwickshire|West Berkshire|West Sussex|Westminster|Wigan|Wiltshire|Windsor and Maidenhead|Wirral|Wokingham|Wolverhampton|Worcestershire|York";

// Now Java script ///////////////////////////////////////////////////////////
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 State', '0');
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, countrySelected) {
var indexSelected=0, countryElement = document.getElementById(countryElementId);
countryElement.length = 0;
countryElement.options[0] = new Option('Select Country', '0');
countryElement.selectedIndex = 0;
for (var i = 0; i < country_arr.length; i++) {
countryElement.options[countryElement.length] = new Option(country_arr[i], country_arr[i]);
if(countrySelected && country_arr[i] == countrySelected){
indexSelected = i+1;
}

}

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

if(countrySelected){
countryElement.selectedIndex = indexSelected;
}
}

////////////////////THIS ENDS THE INCLUDED JAVA FILE /////////////////////

现在我们看看 HTML 实现。

<!-- IS INCLUDED AT HEADER -->
<script src="inc/js/countries.js" type="text/javascript"></script>

<!-- These are my select boxes -->
<select name="country" class="sm_txt_fields" id="country">
</select>

<select name="state" class="sm_txt_fields" id="state">
<script language="javascript">
populateCountries("country", "state", "Canada");
</script>
</select>

代码工作正常,我可以预先选择加拿大,但我也想传递状态变量。不知道如何修改代码。

最佳答案

// countries
var country_arr = new Array( "USA", "Canada", "United Kingdom");

// states
var s_a = new Array();
s_a[0]="";
s_a[1]="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";
s_a[2]="Alberta|British Columbia|Manitoba|New Brunswick|Newfoundland|Northwest Territories|Nova Scotia|Nunavut|Ontario|Prince Edward Island|Quebec|Saskatchewan|Yukon Territory";
s_a[3]="Barking and Dagenham|Barnet|Barnsley|Bath and North East Somerset|Bedfordshire|Bexley|Birmingham|Blackburn with Darwen|Blackpool|Bolton|Bournemouth|Bracknell Forest|Bradford|Brent|Brighton and Hove|Bromley|Buckinghamshire|Bury|Calderdale|Cambridgeshire|Camden|Cheshire|City of Bristol|City of Kingston upon Hull|City of London|Cornwall|Coventry|Croydon|Cumbria|Darlington|Derby|Derbyshire|Devon|Doncaster|Dorset|Dudley|Durham|Ealing|East Riding of Yorkshire|East Sussex|Enfield|Essex|Gateshead|Gloucestershire|Greenwich|Hackney|Halton|Hammersmith and Fulham|Hampshire|Haringey|Harrow|Hartlepool|Havering|Herefordshire|Hertfordshire|Hillingdon|Hounslow|Isle of Wight|Islington|Kensington and Chelsea|Kent|Kingston upon Thames|Kirklees|Knowsley|Lambeth|Lancashire|Leeds|Leicester|Leicestershire|Lewisham|Lincolnshire|Liverpool|Luton|Manchester|Medway|Merton|Middlesbrough|Milton Keynes|Newcastle upon Tyne|Newham|Norfolk|North East Lincolnshire|North Lincolnshire|North Somerset|North Tyneside|North Yorkshire|Northamptonshire|Northumberland|Nottingham|Nottinghamshire|Oldham|Oxfordshire|Peterborough|Plymouth|Poole|Portsmouth|Reading|Redbridge|Redcar and Cleveland|Richmond upon Thames|Rochdale|Rotherham|Rutland|Salford|Sandwell|Sefton|Sheffield|Shropshire|Slough|Solihull|Somerset|South Gloucestershire|South Tyneside|Southampton|Southend-on-Sea|Southwark|St. Helens|Staffordshire|Stockport|Stockton-on-Tees|Stoke-on-Trent|Suffolk|Sunderland|Surrey|Sutton|Swindon|Tameside|Telford and Wrekin|Thurrock|Torbay|Tower Hamlets|Trafford|Wakefield|Walsall|Waltham Forest|Wandsworth|Warrington|Warwickshire|West Berkshire|West Sussex|Westminster|Wigan|Wiltshire|Windsor and Maidenhead|Wirral|Wokingham|Wolverhampton|Worcestershire|York";


// add parameter stateSelected
function populateStates(countryElementId, stateElementId, stateSelected) {
var indexSelected = 0;
var selectedCountryIndex = document.getElementById(countryElementId).selectedIndex;
var stateElement = document.getElementById(stateElementId);
stateElement.length = 0; // Fixed by Julian Woods
stateElement.options[0] = new Option('Select State', '0');
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]);
// find state index
if (stateSelected && state_arr[i] == stateSelected) {
indexSelected = i + 1;
}
}

// set default value if stateSelected passed
if (stateSelected) {
stateElement.selectedIndex = indexSelected;
}
}

// add parameter stateSelected
function populateCountries(countryElementId, stateElementId, countrySelected, stateSelected) {
var indexSelected = 0,
countryElement = document.getElementById(countryElementId);

countryElement.length = 0;
countryElement.options[0] = new Option('Select Country', '0');
countryElement.selectedIndex = 0;

for (var i = 0; i < country_arr.length; i++) {
countryElement.options[countryElement.length] = new Option(country_arr[i], country_arr[i]);
if (countrySelected && country_arr[i] == countrySelected) {
indexSelected = i + 1;
}

}

if (stateElementId) {
if (countrySelected) {
countryElement.selectedIndex = indexSelected;
// call with parameter stateSelected
populateStates(countryElementId, stateElementId, stateSelected);
}
countryElement.onchange = function() {
populateStates(countryElementId, stateElementId);
};
}

if (countrySelected) {
countryElement.selectedIndex = indexSelected;
}
}

// now you can call populateCountries with state
populateCountries("country", "state", "Canada", "Newfoundland");
<select name="country" class="sm_txt_fields" id="country">
</select>
<select name="state" class="sm_txt_fields" id="state">
</select>

关于javascript - 在 javascript 上运行的动态国家/地区选择。如何更改状态的默认选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35629878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com