2016-05-25 44 views
0

我目前通過的結果通過一個DB呼叫建立一個選擇列表中了,然後循環,並生成列表像這樣:如何刪除選項添附的文本選擇列表

$('#CurrentAuthority').children().remove().end(); 
for (i = 0; i < data.length; i++) { 
    $('#CurrentAuthority').append($('<option></option>').attr("value", data[i].CurrentAuthorityId).text(data[i].CurrentAuthority)); 
} 
$('#CurrentAuthority').prepend("<option value='' selected='selected'>--Please Select Authority--</option>"); 

當我嘗試集一個值爲$('#CurrentAuthority').text()我得到的是選定的選項,但也有--Please Select Authority--這是前置的。

是否可以刪除前綴文本?

如果是的話,怎麼樣?

回答

2

你可能想僅查詢所選的選項:

$('#CurrentAuthority > option:selected').text(); 
+1

非常感謝@Martin – kingtreelo

+0

不錯!如果這是您正在尋找的內容,您可以將我的答案標記爲已接受。 – martin

0

使用追加爲第一個文本也: -

$('#CurrentAuthority').children().remove().end(); 
$('#CurrentAuthority').append("<option value='' selected='selected'>--Please Select Authority--</option>"); 
    for (i = 0; i < data.length; i++) { 
      $('#CurrentAuthority').append($('<option></option>').attr("value", data[i].CurrentAuthorityId).text(data[i].CurrentAuthority)); 
     } 

對於所選擇的選項值需要低於使用: -

$('#CurrentAuthority option:selected').val(); 
相關問題