Javascript 循环浏览行并在屏幕上选中哪个复选框
//HtmlJavascript 循环浏览行并在屏幕上选中哪个复选框,javascript,jquery,Javascript,Jquery,//Html function checkPeriods(ePeriodId, eType, eStart, eEnd) { var found = false; #foreach($e in $existing_periods) if(document.getElementById("chk").checked) { $('#periods tbody tr').each(function
function checkPeriods(ePeriodId, eType, eStart, eEnd)
{
var found = false;
#foreach($e in $existing_periods)
if(document.getElementById("chk").checked)
{
$('#periods tbody tr').each(function()
{
var stDt = $('select[name$="start_y"]', this).val() + $('select[name$="start_m"]', this).val();
var enDt = $('select[name$="end_y"]', this).val() + $('select[name$="end_m"]', this).val();
var Ttype = $('select.type', this).val();
if(eType == Ttype && stDt == eStart && eEnd == enDt)
found = false;
});
}
else
{
$('#periods tbody tr').each(function()
{
var stDt = $('select[name$="start_y"]', this).val() + $('select[name$="start_m"]', this).val();
var enDt = $('select[name$="end_y"]', this).val() + $('select[name$="end_m"]', this).val();
var Ttype = $('select.type', this).val();
if(eType == Ttype && stDt == eStart && eEnd == enDt)
found = true;
});
}
#end
if(found == false)
{
alert('Selected Period is not defined. Please define same and check this existing period');
document.getElementById("chk").checked = false;
return false;
}
return true;
}
我想循环浏览数据$existing_periods,我的屏幕包含4个具有相同id的复选框,即chk。我的要求是-
1如果2个复选框选中同一数据行,则应显示验证消息。如果需要获取页面上所有选中复选框的列表,则无需循环任何内容,只需使用此选择器:
<td><input type="checkbox" id="chk" name="chkPeriodID[$e.Periodid]" onclick="checkPeriods('$e.Periodid', '$e.Type1', '$e.Start1', '$e.End1');"/></td><td>$e.Type</td><td>$e.Start</td><td>$e.End</td><td>$e.Requestno</td><td>$e.Proposalno</td><td>$e.Workflowstatus<td>$e.Approved_On</td>
</tr>
但是,您确实需要修复重复ID问题:元素ID在DOM中必须是唯一的。最好将其改为类名:
$('input[type="checkbox"]:checked')
至于你的具体要求,
1如果2复选框选中同一数据行,则应显示验证消息
您没有提供足够的上下文来给出准确的答案,但根据描述,它可能看起来像这样:
$('input.chk:checked');
这里没有关于C:tag Removed的内容。如果有多个元素具有相同的id,则不能使用这些id,因为id必须是唯一的。在查找具有重复id的元素时,浏览器可以执行任何操作。当呈现页面时,将生成具有相同复选框id的4行。我如何循环通过我在html中提供的复选框,这是一个具有id datatble和一个tr的表。在运行时生成的tr编号类似于-row 1-“checkbox”“startDate”“EndDate”。第2行-“复选框”“开始日期”“结束日期”第3行-“复选框”“开始日期”“结束日期”,因此我需要在此进行验证,不允许将两个复选框与相同的开始日期和结束日期进行比较。当然,它在第二次单击复选框时显示验证,而不是第一次单击。这里有生成运行时的所有行的数据。为什么这些信息都不包括在问题中?
$('input.chk:checked');
$('tr').each(function() { // iterate over data rows, assuming they're in a table
if ($(this).find('input.chk:checked').length === 2) { // two checked boxes in this row
// show the validation message for this row:
$(this).find('.classnameForValidationMessage').show();
} else { /* hide it */ }
});