2013-10-25 73 views
0

我有這段代碼將一個列表添加到一個網站(SharePoint)。我添加了一個條件來提醒用戶,如果他們填寫表單時出錯。當我在XML中出現錯誤時,通常使用.find(「errorstring」)。現在我的代碼不斷給我提示,即使當我查看XML錯誤字符串不存在時,仍然存在錯誤。下面的代碼:如果陳述給出不正確的結果。

function createList(){ 
    if(!siteUrl){ 
    alert("You must first select a site"); 
    return; 
    } 
    var listName = $('#listName'); 
    var listDesc = $('#listDesc'); 
    var listTemp = $('#listTemplate'); 
    var siteLists = $('#web_siteLists'); 
    console.log(siteUrl+" - "+listName+" - "+listDesc+" - "+listTemp); 
    $().SPServices({ 
    operation:"AddList", 
    webURL: siteUrl, 
    listName: listName.val(), 
    description: listDesc.val(), 
    templateID: listTemp.find(':selected').val(), 
    completefunc: function(xData, Status){ 
     if($(xData.responseXML).find('errorstring')){ 
     alert('Please fill form correctly'); 
     return; 
     }else{ 
     alert(listName+" created."); 
     siteLists.empty(); 
     RefreshSiteList(siteUrl); 
     listName.val(''); 
     listDesc.val(''); 
     listTemp.val('default');   
     } 
    } 
    }); 
} 

XML響應:http://jsfiddle.net/f6tRw/

我想,我如果條件可能是不正確的說:

如果($(xData.responseXML).find( 'Errorstring,則')) {'請正確填寫表格'); return; }

+0

嘗試沒有$?如果((xData.responseXML).find('errorstring')){...' – brandonscript

回答

2

使用此:

if($(xData.responseXML).find('errorstring').length > 0){ 

通過.find()返回的空集時,有沒有匹配truthy。

+0

那麼如果長度爲0,那麼返回false? – Batman

+0

是的。請參閱http://www.sitepoint.com/javascript-truthy-falsy/ – Barmar

+0

我改變了使用比較的答案,因爲這使得意圖更清晰。 – Barmar