更新:
我遇到了同样的错误。当我点击 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>" )
关于c# - 为什么我收到 NaN is not a valid value of Int32 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6944184/