checkbox 的用法

来源:互联网 发布:祝利荣关键k线指标源码 编辑:程序博客网 时间:2024/04/28 17:46

1 判断checkbox是否选中

if($('#checkbox-id').is(':checked')) {

    // do something

}


2 获取所有选中的checkbox

$("input:checkbox[name='the checkbox name']:checked")

如果是在某一些标签下查找的话,为了防止查找到table #tbTemplate元素以外的checkbox:checked,我们可以这样来限制:

$("table#tbTemplate input:checkbox[name='the checkbox name']:checked")
原生态的用法:

$($("table#tbTemplate input[type='checkbox']"),function(i,checkbox){

     if(checkbox.checked){

          // keep the state. or log this checked......

     }

});

两种方法比较:前者的效率更高


3 select恢复到默认状态: $("select#select1").attr("selected","");

清空下拉列表: $("select#select1").empty();

设置选中项:

$($("select#select1 option"), function(i, option){

       if(option.value="my select option1"){// we also can using the option.text to map the option

            option.selected=true;

       }

});

0 0
原创粉丝点击