繁体   English   中英

为什么我得到NaN不是Int32错误的有效值?

[英]Why am I getting a NaN is not a valid value of Int32 error?

更新:

我得到了同样的错误。 当我点击x ,它应该返回{id:23},但它返回{id: NaN} 如果我删除三元运算符,此问题将自行纠正。


我对我的网页进行了更改:

就是这样:

$( "#notifications" ).prepend( "<div class='notification' id='n" + notifications.d[i].id + "'><span class='notification_text'>" + notifications.d[i].text + "</span><a href='#' class='notification_button' id='b" + notifications.d[i].id + "' value='x'>x</a></div>" ).show();

并且我将其更改为此,根据notifications.d[i].sticky的值添加了三元条件:

$( "#notifications" ).prepend( "<div class='notification' id='n" + notifications.d[i].id + "'><span class='notification_text'>" + notifications.d[i].text + "</span>" + ( notifications.d[i].sticky ? "" : "<a href='#' class='notification_button' id='b'" + notifications.d[i].id + "' value='x'>x</a>'" ) + "</div>" ).show();

该部分工作正常,如果粘性为真,则不创建x链接。

但是,当我点击任何其他x ,我收到服务器端错误消息:

NaN is not a valid value of Int32

服务器端代码如下所示:

[WebMethod()]
public int CloseNotification(int id) {
    using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"])) {
        using (command = new SqlCommand("update notifications set closed_by = @user where id = @id", connection)) {
            command.Parameters.Add("@id", SqlDbType.Int, 4).Value = id;
            command.Parameters.Add("@user", SqlDbType.VarChar, 4).Value = "abc";

            connection.Open();
            intAffectedRows = command.ExecuteNonQuery();
            connection.Close();
        }
    }

    return intAffectedRows;
}

任何人都知道我为什么会收到这个错误?

我认为我在三元组中犯了一个错误,可能是双重或单引号错误,但我看不到它。

您读取的代码中的代码中存在语法错误

id='b'" + notifications.d[i].id + "' value='x'>x</a>'" ) + "</div>" ).show();

b之后还有一个额外的' 它应该是:

id='b" + notifications.d[i].id + "' value='x'>x</a>'" ) + "</div>" ).show();

编辑

在三元操作结束时,您还需要额外的'

( notifications.d[i].sticky ? "" : "<a href='#' class='notification_button' id='b" + notifications.d[i].id + "' value='x'>x</a>'" )

应该:

( notifications.d[i].sticky ? "" : "<a href='#' class='notification_button' id='b" + notifications.d[i].id + "' value='x'>x</a>" )

在三元运算符中,您有一个单引号</a>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM