2014-09-30 70 views
-2

這裏是我的代碼我怎樣才能在選擇列表中刪除未定義的值

if ($("option:contains('" + data.strRoundName + "')").length == 0) 
     $("#selectRound_Type").append("<option name='round' id=" + data.iRoundId + ">" +    data.strRoundName + "</option>"); 

PFB我的輸出畫面(獲得未定義的值)

getting undefined value

+1

確保有東西在'數據。 strRoundName'這就是爲什麼這個問題正在發生。 – 2014-09-30 11:02:32

+1

可能是你的無限陣列 – dreamweiver 2014-09-30 11:04:17

+0

檢查你的循環爲@dreamweiver說:)如果你有類似 i <= array.length,也許將它改爲i 2014-09-30 11:07:07

回答

1

試試這個:過濾所有的選項,這是將文本==未定義並刪除它。

$("#selectRound_Type").find('option').filter(function(){ 
    return $(this).text()=="undefined"; 
}).remove(); 

或檢查undefined同時使用下面的代碼

if ($("option:contains('" + data.strRoundName + "')").length == 0 
     && data.strRoundName != null 
     && typeof data.strRoundName != "undefined") 
     $("#selectRound_Type").append("<option name='round' id=" 
      + data.iRoundId + ">" 
      + data.strRoundName + "</option>"); 
+0

感謝Bhushan :)其工作正常:) – 2014-09-30 11:43:03

+0

快樂幫助你:) – 2014-09-30 11:43:43

1

試試這個

if ($("option:contains('" + data.strRoundName + "')").length == 0){ 
    if (typeof(data.strRoundName) != "undefined" && data.strRoundName != null) { 
     $("#selectRound_Type").append("<option name='round' id=" + data.iRoundId + ">" + data.strRoundName + "</option>"); 
    }  
} 
+0

這'data.strRoundName!== null'應該是'data.strRoundName!= null',刪除額外' ='符號。 – 2014-09-30 11:08:57

1

試試這個添加的選項:

if ($("option:contains('" + data.strRoundName + "')").length == 0) { 
    if(data.strRoundName) 
     $("#selectRound_Type").append("<option name='round' id=" + data.iRoundId + ">" + data.strRoundName + "</option>"); 
} 
+0

這可能是簡單地掩蓋問題而不是修復問題的好方法。別。 – 2014-09-30 11:09:13

+0

如果'data.strRoundName'未定義,那麼它不會追加該元素。這是不正確的? – deepakb 2014-09-30 11:10:42

+0

你沒有聽我說話。 – 2014-09-30 12:12:32

相關問題