博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
转:在Nginx上配置多个站点
查看>>
javascript 技巧总结积累1-108条(正在积累中)
查看>>
为什么尽量避免使用 CSS 表达式
查看>>
[转]这才是真正的3D显示!Leap Motion推出次毫米级3D手动控制技术,让人手和影像融为一体...
查看>>
poj 3621 Sightseeing Cows 01分数规划
查看>>
bzoj 2821:作诗 分块
查看>>
如何让eclipse恢复默认布局
查看>>
简析.NET Core 以及与 .NET Framework的关系
查看>>
【模板题】欧拉回路
查看>>
QEMU+GDB调试方法
查看>>
洛谷 P1272 重建道路(树形DP)
查看>>
sql
查看>>
ShellExecute与ShellExecuteEx的用法
查看>>
第16课 “远程 Git文档库” 的基础操作
查看>>
总结oninput、onchange与onpropertychange事件的使用方法和差别
查看>>
go语言的特点
查看>>
leetcode : Remove Duplicates from Sorted List II [基础]
查看>>
常用正则汇集
查看>>
关于小范围整数N拆解成2的幂相加的个数
查看>>
基于visual Studio2013解决C语言竞赛题之1044数组处理
查看>>