2017年7月20日 星期四

JavaScript 端判斷 DropDownList 的 Text、Value

JavaScript 真的是不容易理解的東西。
光是要判斷 DropDownList 目前的 Value Text 就快頭昏了....
我這方面的觀念真的很差。

除了之前學到的:
// 取得 select  Text
var SelectedText = $(this).find( ":selected").text();

// 取得 select  Value
var SelectedValue = $( this).val();


最近又學了下面的:

//取得 DropDownList 現在選的是第幾筆
$(#DropDownList_ID).val().length

//取得 DropDownList 現在顯示的文字
$("#DropDownList_ID option:selected").text()

//取得 DropDownList 第一筆
$("#DropDownList_ID").find("option").get(0).selected


/* JavaScript 端的 code */
function fn() {
    if ($("#DropDownList_ID").val().length == 0) {
        alert("現在選的是第一筆");
    }

    if ($("#DropDownList_ID option:selected").text() == "One") {
        alert("選的是One");
    }

    if ($("#DropDownList_ID").find("option").get(0).selected == true) {
        alert("現在選的是第一筆");
    }
}