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

 

posted on 2017-07-07 14:49  fanglibs  阅读(3230)  评论(0)    收藏  举报

导航