I'm unable to perform the desired event.
<?php
include_once 'includes/db.php';
$result = mysql_query('SELECT country,code FROM countries') or die(mysql_error());
echo '<select id="CountryCode">';
echo '<option value="Select">Select</option>';
while ($row = mysql_fetch_array($result))
{
echo '<option value=$row["country"]>'.$row['country'].'</option>';
}
echo '<option value="Other">Other</option>';
echo '</select>';
?>
<input id="country" type="hidden" value="IN"/>
<script>
$(function()
{
$('#CountryCode').val($('#country').val());
});
</script>
Everything works fine. But the desired item is not selected in the select box
You need to set the Attribute "selected" of the option to "selected". Try this (untested):
$("#CountryCode").val($('#country').val()).attr('selected','selected');
The echo
in your while()
loop should look like this:
echo '<option value="'.$row["country"].'">'.$row['country'].'</option>';
Your string above was printing a literal $row["country"]
instead of the value that it contained.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.