2013-01-12 38 views
0

我想设置在在我看来条件的div,但我不知道我该怎么做它。如果从控制器请求到达忠实于他,那么成功的div否则出现失败div。当时我只是在警报框中打印真假。 这是我的看法:代码点火器显示的div否则显示不同的DIV在视图

   <div class = "success">operation done successfully </div> 
      <div class = "failiour">operation done successfully </div> 
    //these are the two divs on which i want to apply a condition 

    <form> 



      </form>   

     <script type="text/javascript"> 
     $('#btn').click(function() { // $("#form").serialize() 

var item_name = $('#item_name').val(); 
var cat_id = $('#selsear').val(); 

if (!item_name || item_name == 'Name') { 
    alert('Please enter Category Name'); 
    return false; 
} 

var form_data = { 
     item_name: $('#item_name').val(), 
     cat_id: $('#selsear').val(), 


}; 

$.ajax({ 
    url: "<?php echo site_url('itemsController/additems'); ?>", 
    type: 'POST', 
    data: form_data, 
    dataType: 'json', 
    success: function(msg) { 
     if(msg.res == 1) 
     { 
      alert(true); 
     } 
     else{ 
     alert(false);    
      } 


    } 
}); 

return false; 
     }); 


    </script> 

我的控制器在您的成功和失败的div通过CSS

 function additems(){ 

    //getting parameters from view 
    $data = array(
      'item_name' => $this->input->post('item_name'), 
      'cat_id' => $this->input->post('cat_id') 

    ); 


    //$is_ajax = $this->input->post('ajax'); //or use this line 
    //$this->input->is_ajax_request(); 



$result = array(); 
$this->load->model('itemsModel'); 
$query = $this->itemsModel->addItemstoDB($data); 

if ($query){ //&& any other condition 

    $result['res'] = 1;//process successful - replace 1 with any message 
} 
else 
{ 
    $result['res'] = 0;//process failed - replace 0 with any message 
} 
echo json_encode($result);//at the end of the function. 
} 



    } 

回答

0

设置显示没有再做到这一点:

 
$.ajax({ 
    url: "", 
    type: 'POST', 
    data: form_data, 
    dataType: 'json', 
    success: function(msg) { 
     if(msg.res == 1) 
     { 
      $(".success").fadeIn(500).delay(2000).fadeOut(500); 
     } 
     else 
     { 
      $(".failiour").fadeIn(500).delay(2000).fadeOut(500); 
     } 


    } 
}); 
+0

确定

//supposing you are getting the result in response varible if(response == true) // simply write: if(response) { document.getElementById('success').style.display='block'; document.getElementById('failure').style.display='none'; } else { document.getElementById('success').style.display='none'; document.getElementById('failure').style.display='block'; } 

//添加ID来的div并添加样式块/无以及如何在1 0或2秒后隐藏div – mynameisjohn

+0

我已更新我的答案。核实。 –

+0

感谢...这是工作太 – mynameisjohn

0

第一隐藏的div

<div class = "success" style="display:none;">operation done successfully </div> 
<div class = "failiour" style="display:none;">operation done successfully </div> 

那么如果你是GET reuests多次

$.ajax({ 
    url: "", 
    type: 'POST', 
    data: form_data, 
    dataType: 'json', 
    success: function(msg) { 
     if(msg.res == 1) 
     { 
      $(".success").show(); 
      $(".failiour").hide(); 
     } 
     else 
     { 
      $(".failiour").show(); 
      $(".success").hide(); 
     } 


    } 
}); 
+1

thanksssssssssssss男孩它就像一个魅力..而再加上它是最简单的方法..谢谢你们大家 – mynameisjohn

0

在脚本中,根据您的要求

<div id="success" style="display:block;"></div> 

<div id="failure" style="display:none;"></div> 
+0

如何我得到的回应?因为我收到控制器的回应是'1'这是一个JSON类型...是JavaScript把这个作为回应...? – mynameisjohn

+0

我认为你在数据变量中获得响应。只需检查(数据) – cartina