使用JavaScript动态选择国家城市

dynamic selection country city using javascript

本文关键字:国家 城市 选择 动态 JavaScript 使用      更新时间:2023-09-26

我这样编写了动态选择国家/地区的代码。代码在Mozila,Chrome,Opera,Safari和Internet Explorer 9上运行良好,但国家城市代码的动态选择不适用于Internet Explorer 8及更早版本。

<form method=post id="formname" name="formname" action=eaccountdb.php 
      enctype="multipart/form-data"  onsubmit="return Validate();">
  <table class="style2">
    <tr>
      <td>
        <table align="left" width="100%">
          <tr>
            <td align="left">
              <label for="country">Country*</label>
      <?php
      $country = $_GET['country'];
      if ($country == null)
      {
          $data = mysql_query("select * from country where countryname !='$country'");
          echo "
                <select name='country' style='width:150px' id='country' 
                        onchange='show_country(this.value);'>
                  <option>Select Country</option>";
          while ($info = mysql_fetch_array($data))
          {
              echo "<option>". $info['countryname']."</option>" ;
          }
          echo "</select>";
      }
      ...

猜测一下,我会说这是因为您没有为选项元素提供value属性。在符合 W3C 标准的浏览器中,没有 value 属性的选项的值是选项的文本。不幸的是,IE 8及更低版本没有遵循标准的这一特定部分。简单的答案是在每个选项中放置一个值,如下所示:

  echo "<option value=". $info['cityname'].">". $info['cityname']."</option>" ;