博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jqurey 遍历 div内的所有input单选复选按钮并判断是否选中及Attr(checked)无效的解决...
阅读量:4671 次
发布时间:2019-06-09

本文共 6589 字,大约阅读时间需要 21 分钟。

关于页面前面标签

    @{ foreach (var item in vote) { if (!string.IsNullOrEmpty(item.Img)) {
  • @if (!string.IsNullOrEmpty(item.VoteText)) { } else { }
    @if (!string.IsNullOrEmpty(item.VoteText)) {
    @item.Title } else {
    @item.Title }
  • } else {
  • @if (!string.IsNullOrEmpty(item.VoteText)) { @item.Title } else { @item.Title }
  • } } }

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方法去获得特性

 

jQuery RemoveAttr(checked)之后再Attr(checked)属性无效果

 

当我们升级JQ1.6到更高的版本时,attr(‘checked‘); 返回的是checked或者是undefined,不是原来的true和false了,将attr属性改用prop可解决

 

 

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

 

全选与反选及批量删除

//全选和反选    $("#CheckAll").click(function () {        var flag = $("#CheckAll").is(":checked"); //判断全选按钮的状态         $("input[name=checkitem]").prop("checked", flag);    });    //删除多条信息    function DeleteList() {        var array = new Array();        $.each($("input[name=checkitem]:checked"), function (index, content) {            var iindex = $("input[name=checkitem]:checked")[index].value;            array.push(iindex);        });        if (array.length < 1) {            alert("请选择要删除的项!");            return false;        } else {            var delIds = array.join(",");            if (confirm("你确定删除吗?")) {                $.ajax({                    url: '@Url.Action("DelSortProduct", "SortProduct")',                    type: 'post',                    dataType: 'json',                    contentType: 'application/json',                    data: "{ \"ids\":\"" + delIds + "\"}",                     success: function(result) {                        if (result.DoFlag) {                            alert(result.DoResult);                            location.reload();                        } else {                            alert(result.DoResult);                        }                    },                    error: function(data) {                        alert(data.responseText);                    }                });            }        }    }    $(".setIsDel").click(function () {         var id = $(this).prop("id");         if (confirm('确定删除?')) {            $.ajax({                url: '@Url.Action("DelSortProduct", "SortProduct")',                data: { "ids": id },                //data: "{ \"ids\":\"" + id + "\"}",                success: function(res) {                    if (res.DoFlag) {                        alert(res.DoResult);                        location.reload();                    } else {                        alert(res.DoResult);                    }                },                error: function (data) {                    alert(data.responseText);                }            });        }     });
View Code

 

 

http://www.cnblogs.com/pepcod/archive/2012/07/03/JavaScript.html

http://www.cnblogs.com/shenyixin/archive/2012/12/11/2813221.html

 

转载于:https://www.cnblogs.com/shy1766IT/p/4482845.html

你可能感兴趣的文章
MongoDB 3.2 从安装到使用。
查看>>
lxc 一些有用的资源
查看>>
c# 内部类使用接口IComparer实现排序
查看>>
spring boot 项目搭建时,各个依赖的作用
查看>>
SaltStack
查看>>
提高你的Java代码质量吧:不要让类型默默转换
查看>>
HTML5 audio标签 打造属于自己的音乐播放器
查看>>
【第二组】典型场景:用户上传自定义谜题,工作序号:002,2017/7/6
查看>>
A Fast and Scalable Web Platform by Extending NGINX with Lua
查看>>
5.6 在线DDL (online DDL)详解
查看>>
电容计算公式
查看>>
MySQL高可用架构故障自动转移插件MHA
查看>>
recv send 阻塞和非阻塞
查看>>
网络丢包分析
查看>>
打印LIS
查看>>
剑指offer第2章学习(2)
查看>>
java后台验证码的生成
查看>>
Bootstrap辅助类
查看>>
vue项目的骨架及常用组件介绍
查看>>
Spring使用外部的配置文件
查看>>