0

我想在我的视图中设置条件中的 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>

我的控制器

     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.
}



   }
4

3 回答 3

0

首先隐藏你的div

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

那么如果你多次获得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();
        }


    }
});
于 2013-01-12T04:59:01.210 回答
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并根据您的要求添加样式块/无

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

<div id="failure" style="display:none;"></div>
于 2013-01-12T04:59:42.323 回答
0

在成功和失败 div 中通过 css 设置显示无,然后执行以下操作:

$.ajax({
    网址:“”,
    类型:'POST',
    数据:form_data,
    数据类型:'json',
    成功:功能(味精){
        如果(msg.res == 1)
        {
            $(".success").fadeIn(500).delay(2000).fadeOut(500);
        }
        别的
        {
            $(".failiour").fadeIn(500).delay(2000).fadeOut(500);
        }


    }
});
于 2013-01-12T04:55:07.870 回答