0

我有一个正在使用的插件,它使用数组创建美国各州的下拉列表,然后使用循环填充选择框。这是数组

//Define the US State Array to be used to generate the STATE select box
  $usStates = array(
    "AL" => "Alabama",
    "AK" => "Alaska",
    "AZ" => "Arizona",
    "AR" => "Arkansas",
    "CA" => "California",
    "CO" => "Colorado",
    "CT" => "Connecticut",
    "DE" => "Delaware",
    "FL" => "Florida",
    "GA" => "Georgia",
    "HI" => "Hawaii",
    "ID" => "Idaho",
    "IL" => "Illinois",
    "IN" => "Indiana",
    "IA" => "Iowa",
    "KS" => "Kansas",
    "KY" => "Kentucky",
    "LA" => "Louisiana",
    "ME" => "Maine",
    "MD" => "Maryland",
    "MA" => "Massachusetts",
    "MI" => "Michigan",
    "MN" => "Minnesota",
    "MS" => "Mississippi",
    "MO" => "Missouri",
    "MT" => "Montana",
    "NE" => "Nebraska",
    "NV" => "Nevada",
    "NH" => "New Hampshire",
    "NJ" => "New Jersey",
    "NM" => "New Mexico",
    "NY" => "New York",
    "NC" => "North Carolina",
    "ND" => "North Dakota",
    "OH" => "Ohio",
    "OK" => "Oklahoma",
    "OR" => "Oregon",
    "PA" => "Pennsylvania",
    "RI" => "Rhode Island",
    "SC" => "South Carolina",
    "SD" => "South Dakota",
    "TN" => "Tennessee",
    "TX" => "Texas",
    "UT" => "Utah",
    "VT" => "Vermont",
    "VA" => "Virginia",
    "WA" => "Washington",
    "WV" => "West Virginia",
    "WI" => "Wisconsin",
    "WY" => "Wyoming"
    );

和循环...

//Loop through the state array to build the state select box
   foreach( $usStates as $key => $value ) {
    $selected = "";
    if($_POST['orderState'] == $key) $selected = "selected";
    $wpgft_post_content .= '<option value="'.$key.'" ' . $selected . '>'. $value .'</option>';
    }
    $wpgft_post_content .='</select>';
    if($error_state) $wpgft_post_content .='<br /><span class="wpgft_error">'.$error_state.'</span></dd>';
   } else {
    $wpgft_post_content .='<dt>State / Province / Region:</dt>';
    $wpgft_post_content .= '<select name="orderState"'.$ro.'>';

如果我需要将科罗拉多州设置为默认州,我应该在哪里/如何做到这一点?

4

2 回答 2

0
$wpgft_post_content .= 
    '<option value="'.$key.'" ' . ($key==='CO'?'selected':'') 
     . '>' . $value . '</option>';
于 2013-04-05T19:14:37.790 回答
0

设置另一个变量,它要么是你的 $_POST 值,要么是 colorado,如果它是空的。然后在循环中使用该 var 来检查/设置selected属性:

if (isset($_POST['orderState']) && !empty($_POST['orderState'])) {
    $state = $_POST['orderState'];
} else {
    $state = 'CO';
}

foreach($arr as $key => $val) {
    $selected = ($state == $val) ? 'selected': '';
}
于 2013-04-05T19:16:10.730 回答