【问题标题】:Ajax - compare the value returned by success functionAjax - 比较成功函数返回的值
【发布时间】:2015-12-21 04:46:27
【问题描述】:

我想在ajax请求成功时显示一个表格行。

    success : function(data)
                    {
                        if(data.status === "success")
                        {
                            $(parent_row).remove();
                            alert_box("success", "<?php echo 'keyword deleted successfully'; ?>", "#msg");

                       //display the table row
                            if(data.result == 0)
                            {
                                $("#no_result").show();
                            }
                        }

                    }

我想将成功函数返回的值与整数进行比较,但上面的代码不起作用我还想显示 id 为“#no_result”的表行。

这是 console.log(data) 结果 {"status":"success","re​​sult":0}

这是我的控制器功能。

     public function delete_keywords()
{
    $this->load->model(array('Keywords','Keyword_topics'));
    $keyword = $this->input->post('keyword',TRUE);
    if(isset($keyword) && $keyword != '')
    {

        $this->Keywords->delete_records(array('kwd_id' => $keyword['k_id']));

        $this->Keyword_topics->delete_records(array('tkw_kwd_id' => $keyword['k_id']));
        $get_all_keywords = $this->Keywords->get_all_records();

        $count_ext_kwd=count($get_all_keywords);
        echo json_encode(array('status' => 'success','result' => $count_ext_kwd));

    }

}

我遇到了一个问题,我有一个包含插入数据的表(id,关键字,主题),如果在表中没有找到记录显示“没有找到记录”。当只插入一个(行)数据时,然后刷新页面,删除数据后..消息不显示。

【问题讨论】:

  • 试试parseInt(data.result) == 0
  • 如果您console.log(data) 并查看数据是如何从服务器发送的,很可能是json,您可能会有所了解。此外,如果您也想进行类型匹配,请使用 === 代替 ==。如果数据是json,可以使用obj = JSON.parse(data),然后分别使用obj.statusobj.result访问statusresult中的值
  • alert_box 是否出现
  • 现在大多数浏览器都有一个开发工具,您可以在其中单步执行 javascript 代码并查看从 ajax 命令返回的 http 消息。 “ajax结果{“status”:“success”,“result”:0}”是模棱两可的,那是数据的价值吗?另外,如果这是成功回调,为什么还需要再次检查状态?
  • @mane console.log(data) 值为 Object {status: "success", result: 0}

标签: jquery ajax


【解决方案1】:

您需要使用 parseJSON 来解析响应数据。要显示表格行,首先控制台'#no_result'。这样您就可以知道它是否存在。最后我们可以使用'.show()'。因为您的元素可能不会出现。

success : function(data)
                    {
                        // add this line to your code 
                        parsedObj = jQuery.parseJSON(data); 
                        // use parsedObj below
                        if(parsedObj.status === "success")
                        {
                            $(parent_row).remove();
                            alert_box("success", "<?php echo 'keyword deleted successfully'; ?>", "#msg");

                       //display the table row
                            if(parsedObj.result == 0)
                            {
                                //before use '.show()' check whether element presents. 
                                 console.log("#no_result");
                                // If element presents use '.show()' else, find its parent and finally use '.show()'.  
                                //$("#no_result").show();
                            }
                        }

                    }

【讨论】:

  • 当我检查 console.log($("#no_result"));结果,将数据插入表中并删除然后“[tr#no_result,上下文:文档,选择器:“#no_result”]”,但刷新页面后,删除记录,消息不显示,现在控制台.log($("#no_result")) 结果是 "[context: document, selector: "#no_result"]"
  • 两点,1.jQuery.parseJSON对你有帮助吗? 2.我猜表格行礼物。所以试试 $("#no_result").show()
【解决方案2】:

然后你可以试试这样的东西,希望它有效

success : function(data)
                    {
                        var resObj = JSON.parse(data);
                        if(resObj.status === "success")
                        {
                            $(parent_row).remove();
                            alert_box("success", "<?php echo 'keyword deleted successfully'; ?>", "#msg");

                       //display the table row
                            if(resObj.result === 0)
                            {
                                $("#no_result").show();
                            }
                        }

                    }

【讨论】:

    猜你喜欢
    • 2014-03-27
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    相关资源
    最近更新 更多