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

 我来答
就烦条0o
推荐于2019-11-14 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46483
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
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方法去获得特性

$("#cb").attr("tagName"); //undefined
$("#cb").prop("tagName"); //INPUT
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式