使用 SPservices 检查列表中是否存在当前用户

using SPservices to check current user exists in list

我用 jquery/spservices 创建了一个脚本,用于检查当前登录的用户 ID 是否出现在自定义列表中名为 userid 的列中。如果成功,它会 returns 提示当前用户已找到。

但是,如果找不到用户 ID,我想要另一个提醒,说类似 "you haven't registered yet" 的内容。它在查找用户 ID 的位置时确实有效,但似乎无法判断它不存在。

如有任何帮助,我们将不胜感激:)

代码如下:

var userName = $().SPServices.SPGetCurrentUser({
fieldName: "UserName"});var query = '<Query>' +
'<Where>' +
'<Eq>' +
'<FieldRef Name="userid" />' +
'<Value Type="User">' + userName + '</Value>' +
'</Eq>' +
'</Where>' +
'</Query>';$(document).ready(function() {
$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "test",
    CAMLViewFields: "<ViewFields><FieldRef Name='userid' /></ViewFields>",
    completefunc: function(xData, Status) {
        $(xData.responseXML).SPFilterNode("z:row").each(function() {
            if (userName == $(this).attr("ows_userid")) {
                alert("current user found");
            } else {
                alert("You need to register before accessing..");
            }
        });
    }
});});</script>

你能试试下面的完整功能吗:

completefunc: function(xData, Status) {

    var matchFound = false; //define flag

    //loop through all rows
    $(xData.responseXML).SPFilterNode("z:row").each(function() {
        if (userName == $(this).attr("ows_userid")) {
            matchFound = true; //set the flag to true indicating matched record
        } 
    });

    if(matchFound)
        alert("current user found");
    else
        alert("You need to register before accessing..");
}