js獲取select下拉框選中的的值和判斷checkbox是否選中狀態


html:

<select id="lib_select"  name="">   

  <option   value="1">text1</option>   
  <option   value="2">text2</option>   
</select>

<input type="checkbox" value="1"  id="lib_checkbox" />

javascript原生的方法:

var  libselect=document.getElementById("lib_select");

var index=libselect.selectedIndex;        //拿到選中項(option)的索引(index)

libselect.options[index].value;             //拿到選中項options的value

libselect.options[index].text;    //拿到選中項options的text

var is_checked = document.getElementById("lib_checkbox").checked;           //  true or false

jquery方法

var options=$("#lib_select option:selected");

var _value=options.val();

var _text = options.text();

var is_checked = $("#lib_checkbox").is(':checked');                   //  true or false

or    

var is_checked = $("#lib_checkbox").get(0).checked ;   //  true or false

注意:用.attr('checked') 方法不能動態的取到checked狀態的, 

var is_checked = $("#lib_checkbox").attr('checked');    //   如果設置了checked屬性,則返回checked;  沒設而返回 undefined

 


免責聲明!

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



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