select options of a select element that loaded on runtime

I am using the jQuery to remove options of select and add new option. It is working fine and options change dynamically. However, when i want to select an option do not work. I am using val() property to get specific option. Here is a sample of the code:

html code :

<select id="cmb_signer"  name="signer" class="right"  style="width:100%"></select>

jquery code to change options:

$.post('php/fetch_signer.php',{emptype:emptype},function(data){
   $('#cmb_signer').empty().append('<option value="0">select...</option>' + data);
});

the fetch_signer.php file:

<?php
  include('../../../php/config.php');
  $type=$_POST['emptype'];

  $sql=mysql_query("SELECT * FROM tbl_modiran WHERE emzagroup=$type");
  $count=mysql_num_rows($sql);
  if($count > 0){
    while($row=mysql_fetch_array($sql)){
       $id=$row['id'];
       echo("<option value=".$id.">".$row['name']." ".$row['family']."</option>");
    }
  }else{
     echo("<option value='0'>noting to show</option>");
  }
?>

and jquery code to change the options:

$('#cmb_signer').val($('#modirname'+hokmid).text());

or:

$('#cmb_signer').val(6);

or:

$('#cmb_signer').val("6");

Answers 1

Related Articles