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