删除字符串中的“NaN”

时间:2015-08-26 12:23:25

标签: javascript php jquery

有谁知道如何删除字符串上的“NaN”?

我只是这样做:

if(!$request->captcha_token){
    return "Ops! hold your horse cowboy! please tell me that your not a robot. Fill the captcha!";
    exit;
}

在我的ajax成功函数中,我刚刚做了

success: function (data) {
    this_current.parents(".modal-content").find("#error_container").fadeIn(200);
    this_current.parents(".modal-content").find("#error_container").html(
        '<div class="alert alert-danger overflow_auto" role="alert">' + '<span class="extend display_block align_left margin_right7px"><i class="fa fa-exclamation-triangle"></i></span>' + +'<span class="align_left display_block">' + data + '</span></div>');
}

但响应字符串总是有NaN,如

  

纳米孔多孔硅!抓住你的马牛仔!请告诉我你不是机器人。填写验证码!

我尝试使用

删除它
data.replace('NaN', '')

但不幸的是它没有用,有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

当您应用错误的解析时,NaN值会发生。

像: 的 parseFloat(&#34;杰夫&#34)

它将为您提供NaN以检查您可以使用的值

<强> isNaN(parseFloat(&#34;杰夫&#34))

在你的代码里面我认为这行中的错误与数据无关。代码中的错误:

你在这里:

 "<span>"+ +"</span><span>"+data+"</span>";

这里+运算符之间没有变量。

试试这个:

"<span>"+data+"</span>";

如果它不起作用,那么您可以对您的响应数据应用条件,您可以通过此检查NaN值:

if(isNaN(data)){ return " ";}