2014-01-22 172 views
0

我在從codeigniter複選框中檢索數據時遇到問題。 我的看法是Ajax返回數據錯誤

<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
     <script> 
       var base_url = '<?=base_url()?>'; 

        function test() 
        { 

         var country = document.forms["fetch_country"].country; 
         var countryText = '' 
         for (i=0;i<country.length;i++) 
          { 
          if (country[i].checked) 
          { 
          countryText = countryText + country[i].value + "," 

          } 
          } 
         // alert(countryText) 

           var type= countryText; 
          //  alert (type);     

         $.ajax({ 
          url: base_url+'/index.php/ajax_test/index', 
          type : 'POST', //the way you want to send data to your URL 
          data : {'type' : type}, 
          success : function(result){ //probably this request will return anything, it'll be put in var "data" 
           alert (type); 
           $('#div1').html(result); //jquery selector (get element by id) 

          } 
         }); 
          // alert ("countryFinalValue is " + id); 
        } 

     </script>    
</head>     
        <body> 


        <form id="fetch_country"> 


        <div id="new"> 
        <input id="country" type="checkbox" onclick="test()" value="1">India<br/> 
        <input id="country" type="checkbox" onclick="test()" value="2">USA<br/> 
        <input id="country" type="checkbox" onclick="test()" value="3">UK<br/> 
        <input id="country" type="checkbox" onclick="test()" value="4">China<br/> 

        </div> 
        </form> 

         <div id="div1"></div> 
        </body> 

,我的控制器是:

<?php 

class ajax_test extends CI_Controller 
{ 
function __construct() 
{ 

    parent::__construct(); 
    $this->load->view('view_test'); 
} 

public function index() 
{ 
    //$this->load->view('view_test'); 
    $id = $this->input->post('type'); 
     echo $id; 
    //$this->load->view('view_test' , $id); 

} 


} 

?> 

我面對的是我得到選中的複選框的值的問題,但與價值我得到一整套複選框反覆我的看法 。

Output

+0

你爲什麼使用'jquery 1.3.2'?其過時的伴侶 – Satpal

+0

我可以獲得替代解決方案嗎? –

回答

0

請您在列表生成這樣

<input id="country" type="checkbox" data-value="1">India<br/> 
<input id="country" type="checkbox" data-value="2">USA<br/> 
<input id="country" type="checkbox" data-value="3">UK<br/> 
<input id="country" type="checkbox" data-value="4">China<br/> 

您的測試控制器是罰款,但現在稍後添加此同構造函數

public function __construct() { 
    parent::__construct(); 
    if (!$this->input->is_ajax_request()) { 

     redirect(); //make sure that user is redirected when request is not AJAX 

    } 

    $this->output->enable_profiler(FALSE); //force to disable profiler 
} 

你的JavaScript文件應該是大把就像在頁面頂部的某處定義base_url一樣

$(document).ready(function(){ 
    $('#country').click(function() { 
      var value = $(this).data("value"); 
      $.ajax({ 
       type  : 'POST', //Method type 
       url   : base_url + '/index.php/ajax_test/', 
       data  : {type: value}, //post data 
       success  : function(data) { 
        console.log(data); 
       } 
      }); 
     }); 
}); 
+0

不是這不工作解決方案! –

+0

Chillout一點點,你會得到錯誤嗎? – Kyslik