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

jqurey 遍历 div内的所有input单选复选按钮并判断是否选中

时间:2015-05-06 19:31:03      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

关于页面前面标签

<ul>
                            @{
                    foreach (var item in vote)
                    {
                        if (!string.IsNullOrEmpty(item.Img))
                        {
                            <li class="vop">
                                @if (!string.IsNullOrEmpty(item.VoteText))
                                {
                                    <a href="votedetail?cid=@classid&no=@item.Id">
                                        <img src="@item.Img" />
                                    </a>
                                }
                                else
                                {
                                    <img src="@item.Img" />
                                }
                                <div class="votesubject">
                                    <input id="@item.Id" name="votetitle" value="@item.VoteNum" type="@_votetype" />
                                    @if (!string.IsNullOrEmpty(item.VoteText))
                                    {
                                        <a href="votedetail?cid=@classid&no=@item.Id">
                                            <span>@item.Title</span>
                                        </a>
                                    }
                                    else
                                    {
                                        <span>@item.Title</span>
                                    }
                                </div>

                            </li>
                        }
                        else
                        {
                            <li class="vopn">
                                <input id="@item.Id" name="votetitle" value="@item.VoteNum" type="@_votetype" />
                                @if (!string.IsNullOrEmpty(item.VoteText))
                                {
                                    <a href="votedetail?cid=@classid&no=@item.Id">
                                        <span class="vvpt">@item.Title</span>
                                    </a>
                                }
                                else
                                {
                                    <span class="vvpt">@item.Title</span>
                                }
                            </li>

                        }
                    }
                            }
                        </ul>

js可这样判断是否选中

$(".votesubject").find("input").each(function () {
if ($(this).prop(‘checked‘, true)) {
alert($(this).prop("value"))
}
});

 

也可这样

$("input[name=votetitle]").each(function () {
//if (this.checked) {
// alert($(this).val());
//}
var radios = $(this);
for (i = 0; i < radios.length; i++) {
if (radios[i].checked) {
votenum = parseInt(radios[i].value)+1;
votes += votenum + "@";
ids+=$(this).attr("id")+"@"
ischeck = false;
}
}
});

 

我需要的功能js方法:

$(window).ready(function () {
      $("#tj").click(function () {
        //$(".votesubject").find("input").each(function () {
        //    if ($(this).prop(‘checked‘, true)) {
        //        alert($(this).prop("value"))
        //    }
        //  });
        var ids = "";
        var votes = "";
        var votenum;
        var ischeck = true;
        $("input[name=votetitle]").each(function () {
            //if (this.checked) {
            //    alert($(this).val());
            //}          
            var radios = $(this);          
            for (i = 0; i < radios.length; i++) {
                if (radios[i].checked) {                  
                    votenum = parseInt(radios[i].value)+1;                  
                    votes += votenum + "@";                  
                    ids+=$(this).attr("id")+"@"
                    ischeck = false;
                }
            }
        });
        if (ischeck) {
            alert("请勾选选项后再进行提交");
            return false;
        }
        if (!ischeck) {
            if (ids.length > 1) {
                ids = ids.substring(0, ids.length - 1);
            }
            if (votes.length > 1) {
                votes = votes.substring(0, votes.length - 1);
            }          
            $("#votenum").val(votes);
            $("#ids").val(ids);
            alert("感谢您的参与。");
            $("#form1").submit();
        }
    });
    $("#ck").click(function () {
        window.location = "voteview?cid= " + $("#classid").val() + "&id=" + $("#vid").val() + " ";
    });
});

 

附上相关选中的写法

 $("[name=‘checkbox‘]").attr("checked",true);//全选

 $("[name=‘checkbox‘]").removeAttr("checked");//取消全选

 

$(‘#checkbox‘).attr(‘checked‘); 返回的是checked或者是undefined解决办法

 JQ1.6之后,可以通过attr方法去获得属性,通过prop方法去获得特性

 

  1. $("#cb").attr("tagName"); //undefined   
  2. $("#cb").prop("tagName"); //INPUT   

 

jqurey 遍历 div内的所有input单选复选按钮并判断是否选中

标签:

原文地址:http://www.cnblogs.com/shy1766IT/p/4482845.html

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