2013-10-31 100 views
0

我只是想問一下我的問題。我是使用jQuery的初學者。 我的問題是我想從我的jquery ajax請求獲取json字符串,但我不知道如何。因爲我想檢查json字符串值是否正確。如何獲取jSON數據值?

這裏是我的jQuery中的Ajax代碼:

$('#search-btn').on('click',function(){ 

      var query = $("#keyword").val(); 

      var image = "<?php echo base_url()."/resources/loading/loading43.gif"; ?>"; 

      $('#loading').html("<img src='"+image+"' class='loeader' align='center' />"); 

      var query_url = "<?php echo site_url('item_controller/searchItem'); ?>"; 

      $.ajax({ 

       type:'POST', 
       url: query_url, 
       data:{query: $("#keyword").val(), data_filter: $("#keyword").attr("data-selection")}, //how can I get the value of data_filter? and pass it to the controller? 
       dataType:'json', 
       async: false, 
       success:function(d){ 
         //some codes for success. . . 

      }, 
}); 



$("#selection_filter").on('change',function(){ 

    var filter = $("#selection_filter").val(); 

    $("#keyword").attr("data-selection",filter); 
}); 

這是事件的代碼。

<table border="1" style="width: 100%; align: left" cellpadding="10" cellspacing="10" align="left"> 
      <tr> 
       <td colspan="3"> 
        <h5>SEARCH ITEM</h5> 
       </td> 
      </tr> 
      <tr> 
       <td style="width: 15%"> 
        <label>Choose search filter: </label> 
       </td> 
       <td style="width: 25%"> 
        <select id="selection_filter"> 
         <option value="code">ITEM CODE</option> 
         <option value="itemname">ITEM NAME</option> 
        </select> 
       </td> 
       <td> 
        <input type="text" name="keyword" id="keyword" style="width: 80%" data-selection="code" /> <input type="button" value="SEARCH" id="search-btn" class="k-button" style="font-size: 12px" /> 
       </td> 
      </tr> 
     </table> 

這是我訪問控制器(笨)

public function searchItem(){ 

      $query = $this->input->post('query'); 
      $filter = $this->input->post('data_filter'); 

      if($filter == "code"){ 
       $querySearch = "SELECT item_code,item_name from items WHERE item_code LIKE '%".$query."%' GROUP BY item_code"; 
      }else{ 
       $querySearch = "SELECT item_code,item_name from items WHERE item_name LIKE '%".$query."%' GROUP BY item_code"; 
      } 

      $resultSearch = $this->db->query($querySearch); 
      $count = $resultSearch->num_rows(); 

      echo json_encode($resultSearch->result_array()); 
      //echo json_encode($querySearch); 

     } 

我想要得到的是代碼:

在我的AJAX。有一個數據包含關鍵字和數據過濾器的值。現在我需要做的是獲取data_filter值並將其傳遞給控制器​​。

+0

你可以發佈你正在獲取的示例reposnse json? –

+0

好的謝謝你的迴應。我會查一下。 – Jerielle

+0

下面是示例。 – Jerielle

回答

1

你可以得到這樣你的Ajax內響應

$.ajax({ 
      type:'POST', 
      url: query_url, 
      data:{query: $("#keyword").val(), data_filter: $("#keyword").attr("data-selection")}, 
      dataType:'json', 
      async: false, 
      success:function(res){ 
       console.log(res.item_code);//Will give you the item code 
       console.log(res.item_name);//Will give you the item name 
     }, 
}); 
+0

好吧,先生,但我想要的是來自ajax數據的data_filter值。我需要將ajax數據傳遞給控制器​​。 – Jerielle

0

確定了它在我的語法只是一個錯誤。謝謝你們的迴應。