jquery - why add new option empty tag after load from ajax -


i have 2 dropdowns list category , subs. when user changed category dorpdown, second dropdown become reload server :

<select id="cats">   @foreach($all_cat $c)   <option value="{{$c->id}}">{{$c->title}}</option>   @endforeach </select>  <br />  <select id="subs">  </select> 

my jquery code :

$('#cats').on('change', function() {     var cats = $('#cats').val();     $('#subs option').remove();     $.post('{{base_url()}}cp/vitrinsection/getcategoriesbyajax' , {cats:cats , subs:0 , subs2:0 } , function(data){         $("#subs").append(data);         console.log(data);     }) });  $('#subs').on('change', function() {  }); 

everything works fine after use append function empty option alternate added subs dropdown list !

my console log after data :

<option value='1'>a<option><option value='2'>b<option><option value='3'>c<option><option value='4'>d<option> 

my server code :

public function getcategoriesbyajax(){     $posts = $this->input->post();      if($posts['cats'] != 0){         $cat_id = $posts['cats'];         $subs = $this->m_vitrin->getsubcategories();         $value = '' ;         foreach($subs $s ){             if($s->ads_cat_id == $cat_id){                 $value .= "<option value='$s->id'>$s->title<option>";             }         }          echo $value;      }else if($posts['subs'] != 0){      }else{      } } 

after data server :

<select id="subs"> <option value="1">a</option> <option></option>//? <option value="2">b</option> <option></option>//? <option value="3">c</option> <option></option>//? <option value="4">d</option> <option></option> //? </select> 

public function getcategoriesbyajax(){     $posts = $this->input->post();      if($posts['cats'] != 0){         $cat_id = $posts['cats'];         $subs = $this->m_vitrin->getsubcategories();         $value = '' ;         foreach($subs $s ){             if($s->ads_cat_id == $cat_id){                 $value .= "<option value='$s->id'>$s->title</option>"; //close             }         }          echo $value;      }else if($posts['subs'] != 0){      }else{      } } 

from code above need close option change

if($s->ads_cat_id == $cat_id){ $value .= "<option value='$s->id'>$s->title<option>"; } close change $value .= "<option value='$s->id'>$s->title</option>";


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -