jquery 元素文本取值/賦值


select元素選中option值

jq

$("#select1  option:selected").text();

$("select id或class  option:selected").text();

 

js獲取select標簽選中的值

var obj = document.getElementByIdx_x(”testSelect”); //定位id

var index = obj.selectedIndex; // 選中索引

var text = obj.options[index].text; // 選中文本

var value = obj.options[index].value; // 選中值

 

 

單選按鈕(Radio)當前選中項的值

jq

var value  = $('input[name="radioName"]:checked').val(); //獲取被選中Radio的Value值

 

js

<input type="radio" name="DoorCt" value="twoDoor" checked=”true”onclick="getValue()">Two  
<input type="radio" name="DoorCt" value="fourDoor" onclick="getValue()">Four

 

1 按照name屬性獲取該radio的集合

2 遍歷集合中的每一項元素

3 獲取元素的checked屬性,是否為true,為true,返回其value值

function getValue(){  
    // method 1   
    var radio = document.getElementsByName("DoorCt");  
    for (i=0; i<radio.length; i++) {  
        if (radio[i].checked) {  
            alert(radio[i].value)  
        }  
    }  
}

這里使用alert(radio[i].value)是為了直觀的效果,可以使用return radio[i].value 來返回value值。

細看一下getValue方法的實現。

var document.getElementsByName("DoorCt");

根據name屬性,獲取radio的集合,getElementsByName方法是document獲取對象的三個方法之一,獲取到的是集合。

緊接着 for (i=0;i<radio.length; i++) { 對集合中的元素開始遍歷

if (radio[i].checked) { 遍歷每一個元素時,檢查一下元素的checked屬性,是否為true,為true的,則是被選中的,將其值radio[i].value 值返回。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM