在这里使用了一个名为[0]的字段......但实际上我将它们与索引中的+1(到[1],到[2]等)进行'N'次...克隆...同样适用于[0]。从[0]开始,到[0]是结束日期。 我将结束日期的这两个日期比较不小于开始日期。 这里前两个代码块正常工作,因为每个代码块都是一行。但每当我试图为'N'行创建它时,它就不起作用,即最后一段代码不起作用。
我没有办法选择这些元素,因为名称是动态生成的......
Jquery代码是:
$(document).ready(function() {
$("input[name='to[0]']").blur( function() {
if ($("input[name='to[0]']").val() != 'To' && $("input[name='from[0]']").val() != 'From') {
var a1 = $("input[name='from[0]']").val();
var b1 = $("input[name='to[0]']").val();
alert(b1);alert(a1);
if (a1 > b1) {
alert("Invalid Date Range!\nStart Date cannot be after End Date!");
}
}
});
$("input[name='to[1]']").blur( function() {
if ($("input[name='to[1]']").val() != 'To' && $("input[name='from[1]']").val() != 'From') {
var a1 = $("input[name='from[1]']").val();
var b1 = $("input[name='to[1]']").val();
alert(b1);alert(a1);
if (a1 > b1) {
alert("Invalid Date Range!\nStart Date cannot be after End Date!");
}
}
});
$("td.date").click( function() {
var n = $('#table2 tbody>tr').length - 2;
var r=2;
$("td.date>input:first").next().blur( function() {
while(r<n) {
if ($("input[name='to[r]']").val() != 'To' && $("input[name='from[r]']").val() != 'From') {
var a1 = $("input[name='from[r]']").val();
var b1 = $("input[name='to[r]']").val();
alert(b1);alert(a1);
if (a1 > b1) {
alert("Invalid Date Range!\nStart Date cannot be after End Date!");
}
}
r++;
}
});
});
HTML is ..
<td width="15%" align="center" >
<input class="f" style="width:70px" type="text" size="12" name="from[0]" value="From" readonly="readonly" />
<a class="datepicker" href="#"><img alt="Pick a date" src="js/date.gif" border="0" width="17" height="16" /></a>
</td>
<td width="15%" align="center" >
<input style="width:70px" class="f" type="text" size="12" name="to[0]" value="To" readonly="readonly" />
<a class="datepicker" href="#"><img alt="Pick a date" src="js/date.gif" border="0" width="17" height="16" /></a>
</td>
<td width="15%" align="center" class="date" >
<input style="width:70px" type="text" size="12" name="from[1]" value="From" readonly="readonly" />
<a class="datepicker" href="#"><img alt="Pick a date" src="js/date.gif" border="0" width="17" height="16" /></a>
</td>
<td width="15%" align="center" class="date" >
<input style="width:70px" class="f" type="text" size="12" name="to[1]" value="To" readonly="readonly" />
<a class="datepicker" href="#"><img alt="Pick a date" src="js/date.gif" border="0" width="17" height="16" /></a>
</td>
包含来自[1]和[1]的td的最后一行被克隆,然后最后一行被克隆...
答案 0 :(得分:0)
您需要将计数器作为选择器的一部分,但您需要将其硬编码为r
。试试这个:
while(r<n) {
if ($('input[name="to\\[' + r + '\\]"]').val() != 'To' && $('input[name="from\\[' + r + '\\]"]').val() != 'From') {
var a1 = $('input[name="from\\[' + r + '\\]"]').val();
var b1 = $('input[name="to\\[' + r + '\\]"]').val();
alert(b1);alert(a1);
if (a1 > b1) {
alert("Invalid Date Range!\nStart Date cannot be after End Date!");
}
}
r++;
}
答案 1 :(得分:0)
只需更改选择器,即可无需复制代码。试试这个:
//^= means name starts with, so we are selecting all inputs with that matcher
$("input[name^='to['").blur( function() {
//first we get the name in a variable (just for shortness)
var thisName = $(this).prop("name");
//we then get the numbered index, by splitting the id of the current item
var index = thisName.substring(thisName.indexOf("[") + 1).replace("]", "");
//based on the index, we now can get the corresponding "from" field
var correspondingFromField = $("input[name='from[" + index + "]']");
//now we just do your logic:
if ($(this).val() != 'To' && correspondingFromField.val() != 'From') {
var a1 = correspondingFromField.val();
var b1 = $(this).val();
alert(b1);alert(a1);
if (a1 > b1) {
alert("Invalid Date Range!\nStart Date cannot be after End Date!");
}
}
});