JS選中(肯定value值的)radiobutton


1.利用普通JS腳本
        var frm = document.frm

        var rdo_tv = frm.rdo_tv;
        for(i=0;i<rdo_tv.length;i++){
           if(rdo_tv[i].value == 19){
               rdo_tv[i].checked = true;
               break;
            }
        }
       
上面給出的是一個示例,其中rdo_tvradiobuttonname,其中的break不是必須的,爲了節省時間能夠加上。

2.
利用jQuery框架功能
       jQuery("input[@name=avatar][@value=f]").attr("checked",true);
       
或者$("input[@name=avatar][@value=f]").attr("checked",true);
       
使用jQuery這個庫很容易就實現了。須要注意的是,其中的@多適用於老版本的jQuery庫,新版本的能夠不使用@,即:
       jQuery("input[name=avatar][value=f]").attr("checked",true);
       
或者$("input[name=avatar][value=f]").attr("checked",true);

3.
一些相關的知識(涉及到radiocheckbox
       
獲取一組radio被選中項的值
            var item =$('input[@name=items][@checked]').val();
       
獲取select被選中項的文本
            var item =$("select[@name=items] option[@selected]").text();
        select
下拉框的第二個元素爲當前選中值
           $('#select_id')[0].selectedIndex = 1;
        radio
單選組的第二個元素爲當前選中值
           $('input[@name=items]').get(1).checked = true;

     
獲取值:

       
文本框,文本區域:
           $("#txt").attr("value")

       
多選框checkbox
           $("#checkbox_id").attr("value")

       
單選組radio
           $("input[@type=radio][@checked]").val();
       
下拉框select
           $('#sel').val();

     
控制表單元素:
       
文本框,文本區域:
           $("#txt").attr("value",'');//
清空內容
           $("#txt").attr("value",'11');//
填充內容
       
多選框checkbox
           $("#chk1").attr("checked",'');//
不打勾
           $("#chk2").attr("checked",true);//
打勾
           if($("#chk1").attr('checked')==undefined) //
判斷是否已經打勾
       
單選組radio
           $("input[@type=radio]").attr("checked",'2');//
設置value=2的項目爲當前選中項
       
下拉框select
           $("#sel").attr("value",'-sel3');//
設置value=-sel3的項目爲當前選中項
           $("<option value='1'>1111</option><optionvalue='2'>2222</option>").appendTo("#sel")//
添加下拉框的option
           $("#sel").empty()
//清空下拉框

4.
默認選中全部的Checkbox
           $(document).ready(function(){
               jQuery(':checkbox').attr("checked",true)
            });

總結:
       
不少東西都是相通的,知識也是,上面這些知識均可以變變形式,用於其餘功能的實現。app

相關文章
相關標籤/搜索