2017-05-27 61 views
0

我有Ajax響應後空<div><select>要加載裏面的div和option標籤和輸入標籤內選擇。我會怎麼做?如何獲得單獨的輸入標籤和選項標記和負載內的div和內選擇

這裏是響應 enter image description here

我想單獨提取標籤和輸入爲<select><div> &提取選項。 這裏是我的代碼:enter image description here

加上我從我的笨控制器得到這個的回覆::

public function student() 
    { 
     $sch= $this->input->post('st'); 
     $swt=$this->data['school'] = $this->catering_model->selstu($sch); 

     //echo(json_encode($swt)); 
     if(!empty($swt)) 
     { 
      if(($sch == "1")) 
      { 
       echo ('<label class="checkbox-inline"> 
       <input type="checkbox" id="offer1" name="offer" value="Monday">Monday 
       </label>'); 

      } 
      foreach($swt as $in) 
      { 

       echo ('<option value="'.$in->id.'" select="select">'.$in->fname.'&nbsp;'.'</option>'); 


      } 

     } 


     //var_dump($data['school']); 
    } 

我天璣KNW

<div class="form-group"> 
<select id="student" name="student" class="form-control" onchange="getName(this.value)"> 

</select> 
</div> 
<div name="state" id="state"> 



</div> 

<script> 
function getschool() { 
var st = $('#school option:selected').val(); 

$.ajax({ 
    type: "POST", 
    url: '<?php echo base_url();?>Choice/student', 
    type: 'POST', 
     data: { 
      // fname: fname 
      st:st 



     }, 
     dataType: 'text', 
     success: function(data) { 
      alert(data); 
      //alert(data[0].id); 
      // for(k in data){ 
       // alert(data[k].id); 
       $('#student').html(data); 
       //$('#state').html(data.input); 
       $('#state').html(data); 
      // } 
      //$('body').html(data); 
      // console.log(data); 
      //alert(data); 
      //alert("Succesful "); 
      //location.reload(false); 
      //window.location.href = "http://localhost:8080/choicelaunch/Choice/order_fullmenu"; 
     } 

}); 
} 

</script> 

這樣做,我得到的每一件事情裏面DIV等之後爲什麼。我只需要複選框不是非複選框。

+0

任何一個????????? – user3162878

回答

1

你可以使用filter()

$('#student').html($(data).filter('option')); 
$('#state').html($(data).filter('label')); 
+0

m nt得到結果。它什麼都沒顯示 – user3162878

+0

anyidea ???????? – user3162878

+0

jquery error說:「data.find不是函數」 – user3162878

0

我知道這有點棘手。但是,試試這個。

你爲什麼不嘗試添加DIV的內容和控制器選擇內容之間的隔板。這裏我的意思是:

public function student() 
    { 
     $sch= $this->input->post('st'); 
     $swt=$this->data['school'] = $this->catering_model->selstu($sch); 

     //echo(json_encode($swt)); 
     if(!empty($swt)) 
     { 
      if(($sch == "1")) 
      { 
       echo ('<label class="checkbox-inline"> 
       <input type="checkbox" id="offer1" name="offer" value="Monday">Monday 
       </label>'); 

      } 
      echo "@@"; //the separator 
      foreach($swt as $in) 
      { 

       echo ('<option value="'.$in->id.'" select="select">'.$in->fname.'&nbsp;'.'</option>'); 


      } 

然後,成功後收到數據,嘗試分割它。

success: function(data) { 
     alert(data); 
     var res = data.split("@@"); 
     $('#student').html(res[0]); 
     $('#state').html(res[1]); 
    }