天天看點

javascript的select、radio、checkbox取值總結

(1)select取值value 跟擷取<option>和</option>之間的文本

(0)逗比方法取值:隻能取值 不能取<option>和</option>之間的文本

document.getElementById("nimei").value

$("#nimei").val()

(1)dom方法

document.getElementById("K_time");

var tt=t.options[t.selectedIndex].value;

取<option>和</option>之間的文本

var tt=t.options[t.selectedIndex].text;

(2)jq方法

var t=$("#K_time")[0];

var tt=t.options[t.selectedIndex].value;

注意:一定要加[0]  不加0會報錯 

因為這是

不加【0】是jquery對象

加上就是dom對象

取<option>和</option>之間的文本

var tt=t.options[t.selectedIndex].text;

(2)radio跟checkbox的取值

(1)jquery取法

$("input[id='airuikun']:checked").val();

(2)dom取法:

    原理:因為選項很多 值很多 不是一個id 不能用get id

var c = document.getElementsByName("account");

            for(var i=0;i<c.length;i++)

             {

               if(c[i].checked){

                     alert(c[i].value);

  }

             }

繼續閱讀