欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Js獲取單選框radio的幾種方式

 更新時(shí)間:2023年12月02日 10:37:07   作者:widenstage  
這篇文章主要介紹了Js獲取單選框radio的幾種方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Js獲取單選框radio

首先編寫HTML

如下:

    <form id="form1">
        <table  border="0"> 
            <tr>
                <td>年齡段:</td>
                <td>
                    <input type="radio" name="age" value="18" />小于18歲
                    <input type="radio" name="age" value="18-40" checked="checked" />18-40歲
                    <input type="radio" name="age" value="40" />40以上
                </td>
            </tr>
           <tr>
                <td>分?jǐn)?shù):</td>
                <td>
                    <input type="radio" name="score" value="60" />小于60分
                    <input type="radio" name="score" value="60-80" checked="checked" />60-80分
                    <input type="radio" name="score" value="80" />80分以上
                </td>
            </tr>
         </table>
     </form>

方法

       function readradio() {
 
            // 方法一            
            var item = null;
            var obj = document.getElementsByName("age")
            for (var i = 0; i < obj.length; i++) { //遍歷Radio 
                if (obj[i].checked) {
                    item = obj[i].value;                   
                }
            }
            alert(item);
 
            // 方法二 jquery版本在1.3之前 (FF和chrome下無效)
            item = $('input[name=age][checked]').val();
            alert(item);
            
            // jquery 1.3 之后使用
            item = $('input[name=age]:checked').val();
            alert(item);
 
            // 方法三 jquery 讀取多個(gè) 版本在1.3之前 (FF和chrome下無效)
            $("input[type=radio][checked]").each(function() {
                item =  $(this).val();
                alert(item);
            })
            
            // jquery 1.3 之后使用
            $("input[type=radio]:checked").each(function() {
                item = $(this).val();
                alert(item);
            })     
 
        }

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論