js擷取asp.net伺服器端控制項Label,TextBox,RadioButtonList,DropDownList的值

來源:互聯網
上載者:User

在做 BS架構的項目時,經常遇到 需要用js 來調用 asp.net 伺服器端控制項的值。

大多數的 控制項他的值都可以通過js調用它的 value屬性來獲得此控制項的值,但是也有例外的情況。

經常用的 Label控制項。他的值用js就不能通過value屬性來獲得。

    Label控制項 js擷取的執行個體, var text= document.getElementById('Label1').innerText;

假如, 這樣 var text= document.getElementById('Label1').value; 則 text 為Undefined。

而 TextBox 的值

就可以 var text= document.getElementById('TextBox 1').value;

對於,RadioButtonList 與 DropDownList 他們的擷取方式是大不一樣的! 這主要是因為 他們所產生的 html元素不一樣。

DropDownList 的值 擷取 比較簡單:

        var ddlvalue = document.getElementById('ctl00_Contentplaceholder3_ddlFolws').value;
       

而 RadioButtonList 的值擷取 就比較麻煩:

        var value = "";

        var Result = document.getElementsByName('ctl00$Contentplaceholder3$rblResult');
        for (var i = 0; i < Result.length; i++) {
            if (Result.item(i).checked) {
                value = Result.item(i).value;
            }
        }

如果 RadioButtonList 控制項 沒有一個選擇的 那麼 value的值 為空白!

=======關於substring 用法(字串截取)======

function SubstringDemo(){
var ss;                      // 聲明變數。
var s = "The rain in Spain falls mainly in the plain..";
ss = s.substring(12, 17); // 取子字串。
return(ss);                   // 返回子字串。
}

 

轉自:http://kb.cnblogs.com/a/2288883/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.