radio 判断一个元素是否被选中

来源:互联网 发布:指南针软件指标 编辑:程序博客网 时间:2024/06/05 05:29

选中有一个elem元素,现在判断它是否被选中 ,之前我们使用的是 $(elem).atter("checked") 可是有时候不管用。后来使用这种方式就可以了

如果被选中则返回true,未被选中则返回false

<html><head>    <meta name="viewport" content="width=device-width" />    <title>Index</title>    <script src="~/Scripts/jquery-1.10.2.js"></script></head><body>    <div>        <input type="radio" name="fruit" value="苹果" />苹果        <input type="radio" name="fruit" value="橘子" />橘子        <input type="radio" name="fruit" value="香蕉" />香蕉        <button onclick="fun.CheckSelectItem()">提交</button>    </div></body></html><script type="text/javascript">    var fun = {        CheckSelectItem: function () {            $.each($("input[name=fruit]"), function (key, element) {                //使用prop("checked")检查element这个元素是否被选中,如果被选中则返回true,未被选中则返回false                if ($(element).prop("checked")) { // $(element)是将element这个元素转化为jquery元素                    alert("您的选中项是:" + element.value);                }            })        }    }</script>



 //$("input[name='r1']:checked") 获取name属性为r1 且被选中的 input表单   

0 0