admin管理员组

文章数量:1023251

I'm using this Country / State dynamic selection on my code :

/

and here's the demo page : /

but, unfortunately when I create setting / member profile page which I need to display selected country and state so user can see what they already chosen, I can't just put user's selected as 'value' on it.

here's what I see on page source even I have select a country and a state :

Select Country: <select onchange="print_state('state',this.selectedIndex);" id="country" name ="country"></select>

City/District/State: <select name ="state" id ="state"></select>

how to put value on that ? because I did like this :

Select Country:   
<select onchange="print_state('state',this.selectedIndex);" id="country" name ="country" value="Indonesia"></select>

City/District/State: <select name ="state" id ="state"></select>

and still I don't see there's a country selected. thanks!

I'm using this Country / State dynamic selection on my code :

http://bdhacker.wordpress./2009/11/21/adding-dropdown-country-state-list-dynamically-into-your-html-form-by-javascript/

and here's the demo page : http://bdhacker.sourceforge/javascript/countries/

but, unfortunately when I create setting / member profile page which I need to display selected country and state so user can see what they already chosen, I can't just put user's selected as 'value' on it.

here's what I see on page source even I have select a country and a state :

Select Country: <select onchange="print_state('state',this.selectedIndex);" id="country" name ="country"></select>

City/District/State: <select name ="state" id ="state"></select>

how to put value on that ? because I did like this :

Select Country:   
<select onchange="print_state('state',this.selectedIndex);" id="country" name ="country" value="Indonesia"></select>

City/District/State: <select name ="state" id ="state"></select>

and still I don't see there's a country selected. thanks!

Share Improve this question asked Nov 15, 2012 at 14:25 Saint RobsonSaint Robson 5,52518 gold badges74 silver badges121 bronze badges 2
  • Have you included the function and data in your page? <script type="text/javascript" src ="countries-2.0-min.js"></script> (you'll obviously need to update the path to the file) – peterph Commented Nov 15, 2012 at 14:37
  • I have. I can see my dropdown menu works exactly like demo page. but when I add : <select onChange="print_state(\'state\',this.selectedIndex);" id="country" name = "country"><option value="Indonesia" selected>Indonesia</option></select> I still can't see a country selected. – Saint Robson Commented Nov 15, 2012 at 14:44
Add a ment  | 

2 Answers 2

Reset to default 1

try this solution http://jsfiddle/yDAmR/

print_country("country");
$('#country').val('Indonesia');
print_state('state',$('#country')[0].selectedIndex);

The reason you see the USA states for other countries is that the JavaScript is missing a reset of the state drop-down list. Just add "option_str.length=0; // Clear list" in the JS file before you split list array.

I'm using this Country / State dynamic selection on my code :

/

and here's the demo page : /

but, unfortunately when I create setting / member profile page which I need to display selected country and state so user can see what they already chosen, I can't just put user's selected as 'value' on it.

here's what I see on page source even I have select a country and a state :

Select Country: <select onchange="print_state('state',this.selectedIndex);" id="country" name ="country"></select>

City/District/State: <select name ="state" id ="state"></select>

how to put value on that ? because I did like this :

Select Country:   
<select onchange="print_state('state',this.selectedIndex);" id="country" name ="country" value="Indonesia"></select>

City/District/State: <select name ="state" id ="state"></select>

and still I don't see there's a country selected. thanks!

I'm using this Country / State dynamic selection on my code :

http://bdhacker.wordpress./2009/11/21/adding-dropdown-country-state-list-dynamically-into-your-html-form-by-javascript/

and here's the demo page : http://bdhacker.sourceforge/javascript/countries/

but, unfortunately when I create setting / member profile page which I need to display selected country and state so user can see what they already chosen, I can't just put user's selected as 'value' on it.

here's what I see on page source even I have select a country and a state :

Select Country: <select onchange="print_state('state',this.selectedIndex);" id="country" name ="country"></select>

City/District/State: <select name ="state" id ="state"></select>

how to put value on that ? because I did like this :

Select Country:   
<select onchange="print_state('state',this.selectedIndex);" id="country" name ="country" value="Indonesia"></select>

City/District/State: <select name ="state" id ="state"></select>

and still I don't see there's a country selected. thanks!

Share Improve this question asked Nov 15, 2012 at 14:25 Saint RobsonSaint Robson 5,52518 gold badges74 silver badges121 bronze badges 2
  • Have you included the function and data in your page? <script type="text/javascript" src ="countries-2.0-min.js"></script> (you'll obviously need to update the path to the file) – peterph Commented Nov 15, 2012 at 14:37
  • I have. I can see my dropdown menu works exactly like demo page. but when I add : <select onChange="print_state(\'state\',this.selectedIndex);" id="country" name = "country"><option value="Indonesia" selected>Indonesia</option></select> I still can't see a country selected. – Saint Robson Commented Nov 15, 2012 at 14:44
Add a ment  | 

2 Answers 2

Reset to default 1

try this solution http://jsfiddle/yDAmR/

print_country("country");
$('#country').val('Indonesia');
print_state('state',$('#country')[0].selectedIndex);

The reason you see the USA states for other countries is that the JavaScript is missing a reset of the state drop-down list. Just add "option_str.length=0; // Clear list" in the JS file before you split list array.

本文标签: phpSelected CountryState From Dynamic Dropdown ListStack Overflow