2016-11-04 78 views
1

我已經創建了一個包含jquery/spservices的腳本,用於檢查當前登錄的用戶ID是否出現在自定義列表中名爲userid的列中。如果成功,它將返回一個警報,以表示發現當前用戶。使用SPservices檢查列表中是否存在當前用戶

如果找不到用戶標識,但是我希望另一個提醒說「你還沒有註冊」。它在找到用戶標識的位置時工作,但似乎無法確定它不存在。

任何援助將不勝感激:)

這裏是代碼:

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> 

回答

0

你可以嘗試用下面的完整功能:

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.."); 
} 
+0

,完美的工作!非常感謝你 :) – RScott