码迷,mamicode.com
首页 > 其他好文 > 详细

输出选中的值

时间:2017-04-05 01:03:42      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:pre   list   click   orm   form   rip   doc   nts   value   

js输出选中的值:

原生js输出:  

<form action="" method="get" id="list">
    您最喜欢水果?<br /><br />
    <label><input name="Fruit" type="radio" value="1"/>苹果 </label>
    <label><input name="Fruit" type="radio" value="2" />桃子 </label>
    <label><input name="Fruit" type="radio" value="3" />香蕉 </label>
    <label><input name="Fruit" type="radio" value="4" />梨 </label>
    <label><input name="Fruit" type="radio" value="5" />其它 </label>
    <span onclick="sum(‘Fruit‘)">SUM</span>
</form>
<script>
    function sum(t_name){
        var obj=document.getElementsByName(t_name);  //得到名字为t_name的数组
        for(var i=0;i<=obj.length;i++){  //遍历数组的checked值
            if(obj[i].checked){   //如果选中了,输出i       
                console.log(i);      
            }
        }
    }
</script>

 

 

jq输出:

<form action="" method="get" id="list">
    您最喜欢水果?<br /><br />
    <label><input name="Fruit" type="radio" value="1"/>苹果 </label>
    <label><input name="Fruit" type="radio" value="2" />桃子 </label>
    <label><input name="Fruit" type="radio" value="3" />香蕉 </label>
    <label><input name="Fruit" type="radio" value="4" />梨 </label>
    <label><input name="Fruit" type="radio" value="5" />其它 </label>
    <span onclick="sum(‘radio‘)">SUM</span>
</form>
<script src="js/jquery.min.js"></script>
<script>
    function sum(radio){
        var check_val=$("input[type=‘"+radio+"‘]:checked").val(); //找到选中那个数值
        console.log(check_val);
    }
</script>

 

输出选中的值

标签:pre   list   click   orm   form   rip   doc   nts   value   

原文地址:http://www.cnblogs.com/EllenChen/p/6666940.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!