2013-10-25 20 views
0

我有一個刪除按鈕選擇確定或更多列表項目已被選擇,如果是的話,繼續刪除這些項目。如果任何選項已在if語句

我的if語句不斷返回「您必須先選擇您的列表」,即使它們已被選中。

function deleteList(){ 
    if(!siteUrl){ 
    alert("You must first select a site"); 
    return; 
    }else if (siteLists.selectedIndex == null){ 
    alert('You must first select your list(s)'); 
    return; 
    }else{ 
    var arrList = siteLists.val(); 
    var listIndex; 
    var removeConfirm = confirm("Are you sure you want to delete these lists?") 

    if(removeConfirm){ 
     for(listIndex = 0; listIndex<arrList.length;listIndex++){ 
     $().SPSservices({ 
      operation: "DeleteList", 
      webUrl: siteUrl, 
      listName: arrList[listIndex], 
      completefunc: function(){ 
      RefreshSiteList(siteUrl); 
      alert("Selected lists have been deleted"); 
      } 
     }); 
     }   
    } 
    } 
} 

HTML

<select id="web_siteLists" style="width:150px;height:150px;" multiple="multiple"> 
<option value="{041D004F-3BD7-41C2-BE02-F166A6970FDA}">Announcements</option> 
<option value="{92E428BF-6F94-47D5-B9EF-446F62827749}">Calendar</option> 
<option value="{F8F92E1D-4CF0-4E80-B037-1867BD8A35B2}">Links</option> 
<option value="{1E0BD0DF-D4A0-4C27-8F3F-5E4AB4FAFBB4}">Master Page Gallery</option> 
<option value="{DF86CAA5-B365-484D-8792-9771E10768E0}">Team List 3</option> 
<option value="{5FD4247D-3C54-4D9A-8F0F-E4C374D966B0}">Team List 4</option> 
<option value="{E7CBEC72-7A63-414A-8F3C-18579A3FC7D7}">Team List 5</option> 
</select> 
+0

你在哪裏實際上定義'siteLists' – scrappedcola

+0

我有抓住元素待機功能:siteLists = $( '#web_siteLists');這是一個全球變量。否則我會得到一個未定義的。 – Batman

+0

你是否選擇了dom準備好?如果沒有,它將不存在 – scrappedcola

回答

1

既然你指出,在評論中,你的變量siteLists是一個jQuery對象,您需要更改:siteLists.selectedIndex == null){

if(siteLists.get(0).selectedIndex == -1){ 

由於已贏得」沒有那個方法定義爲null。 .get()Retrieve the DOM elements matched by the jQuery object.將null更改爲-1。如果您沒有選擇任何內容,selectedIndex將爲-1。看到這個小提琴:http://jsfiddle.net/R6YM8/

+0

對不起,在這裏做什麼(0)?我看了看文檔,它似乎是一個AJAX調用http://api.jquery.com/jQuery.get/ – Batman

+0

我試過這個,但現在沒有選擇任何東西時不會返回警報 – Batman

+0

http://api.jquery .com/get /有兩個方法命名爲不幸。我已經更新了我的答案以表明這一點。 – scrappedcola

1

「我試過,但現在當什麼也沒有選擇它沒有返回警報」

當選擇任何內容,selectedIndex屬性將是-1

因此:

if (siteLists.prop('selectedIndex') === -1) {/*nothing selected*/} 
+0

這工作,謝謝 – Batman