开发者

Selecting a value in drop down list using javascript

开发者 https://www.devze.com 2023-02-21 21:03 出处:网络
A] Summary of the problem: Trying to pre-select a users country on page load B] Details: 1] I am using maximinds javascript to find out a users county.

A] Summary of the problem:

Trying to pre-select a users country on page load

B] Details:

1] I am using maximinds javascript to find out a users county.

2] I have a drop down list which contains a list of 225 or so countries.

3] Inside the javascript section of my HTMLr page, I am trying to select the users country from the drop-down list.

But the country is not getting selected

C] Code excerpt:

<select name="country_name" id="id_country_name">
 <option value="" selected="selected">---------</option>
 <option value="Afghanistan">Afghanistan</option>
 <option value="Aring land Islands">Aring land Islands</option>
</select>


<!-- including the  geoip javascript library -->
<script src="http://j.maxmind.com/app/geoip.js" type="text/javascript"></script>
<!-- By default, we will select users country -->       
<script type="text/javascript" language="JavaScript">
document.getElementById(geoip_country_name()).selected = true
</script>

Thanks,

[Edit#1]

Tried the following jquery code, but this isnt populating the drop-down list:

<!-- including the  geoip javascript library -->
  <script src="http://j.maxmind.com/app/geoip.js" type="text/javascript"></script>
  <script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script type="text/javascript">
    $(function () {
        $("option[value='" + geoip_country_name() + "']").attr('selected', 'selected');
    });
 </script>

[Edit#2]

Tried the $("#id_country_name").val(geoip_country_name());,

<div id="userDataForm">
  <form method="POST" action="/UserReporting"> 
  <table>
      <!-- Printing the forms for users country, city -->
      <tr><th><label for="id_country_name">Country name:</label></th><td>
      <select name="country_name" id="id_country_name">
    <option value="" selected="selected">---------</option>
    <option value="Afghanistan">Afghanistan</option>
        <option value="Aring land Islands">Aring land Islands</option>
        <option value="Albania">Albania</option>
      </select>
      </td></tr>
      <tr><th>
      <label for="id_city_name">City name:</label></th><td><input type="text" name="city_name" id="id_city_name" /></td></tr>     
 </table>

 <input type="submit" name="report_up" value= "Report Up">
 <input type="submit" name="report_down" value= "Report Down">
 </form>

 <!-- including the  geoip javascript library -->
<script type="text/javascript" language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script src="http://j.开发者_C百科maxmind.com/app/geoip.js" type="text/javascript">
</script>
<script type="text/javascript">
    $(function () {
        $("#id_country_name").val(geoip_country_name());
    });
</script>


The document.getElementById call would only work if each <option> in your HTML had the country name as the id (replacing spaces with "_" or something similar):

<select name="country_name" id="id_country_name">
 <option value="" selected="selected">---------</option>
 <option id="Afghanistan" value="Afghanistan">Afghanistan</option>
 <option id="Aring_land_Islands" value="Aring land Islands">Aring land Islands</option>
</select>

However, I'd lean towards this approach with jquery, as it doesn't require giving every option its own id:

$(function () {
    $("#id_country_name").val(geoip_country_name());
});


Try

document.getElementById('id_country_name').selectedIndex=index;

where index is an integer corresponding to the selection you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消