0

以下是日期选择器的代码。当我们在 from 字段中选择日期时,“to”字段中位于“from”字段下方的所有日期都将被阻止。我想在 for 循环的帮助下生成这些 id,但是当我运行此代码时,文本框仅显示具有“from”和“to”功能的日期选择器。

$(function () {
    var frm = 0;
    var tu = 0;
    var i = 0;
    for (i = 1; i <= 5; i++) {
        frm = "#from" + i;
        tu = "#to" + i;
        $(frm).datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'yy/mm/dd',
            maxDate: '-1',
            onSelect: function (selected) {
                $(tu).datepicker("option", "minDate", selected)
            }
        });
        $(tu).datepicker({
            dateFormat: 'yy/mm/dd',
            changeMonth: true,
            changeYear: true,
            maxDate: '-1',
            onSelect: function (selected) {
                $(frm).datepicker("option", "maxDate", selected)
            }
        });
    }
});

html 由 php 生成 for ($a=1;$a<=$i;$a++) {

echo("
    <li>

    <input type=\"text\" name=\"course_name$a\" class=\"jq_watermark required\" placeholder=\"Course Name\" title=\"$x Certified Course Name\" style=\"width:11.6em;\"> 
    <tab></tab>
    <input type=\"text\" name=\"course_institute$a\" class=\"jq_watermark required\" placeholder=\"Institute Name\" title=\"$x Institute Name\" style=\"width:11.6em;\"> 
    <tab></tab>
        <br />
    <input type=\"text\" id=\"from$a\" name=\"course_start_date$a\" class=\"required\" placeholder=\"Certified Course Start Date\" title=\"$x Certified Course Start Date\" style=\"width:11.6em;\" readonly=\"readonly\"> 
    &nbsp to&nbsp
    <input type=\"text\" id=\"to$a\" name=\"course_end_date$a\" class=\"required\" placeholder=\"Certified Course End Date\" title=\"$x Certified Course End Date\" style=\"width:11.6em;\"> 
    <br />
     <input type=\"text\" name=\"course_city$a\" class=\"required\" placeholder=\"City\" title=\"$x Training Institute City Name!\" style=\"width:11.6em;\"> 
    <tab></tab>
     <input type=\"text\" name=\"course_country$a\" class=\" required\" placeholder=\"Country\" title=\"$x Training Institute Country Name!\" style=\"width:11.6em;\"> 
    <tab></tab>
    <button title=\"Your Training Certificate Scanned Picture, Please!\">Upload Training Certificate</button>
    </li> 
    ");

}

4

1 回答 1

0

由于您的匿名函数创建的闭包,您始终拥有相同的frm/ tu(应该是tobtw.)变量。通过引入一个接收这些变量作为参数的自调用匿名函数,您可以创建新的变量来避免这个问题:

$(function () {
    var frm = 0;
    var tu = 0;
    var i = 0;
    for (i = 1; i <= 5; i++) {
        frm = "#from" + i;
        tu = "#to" + i;
        (function (frm, tu) {
            $(frm).datepicker({
                changeMonth: true,
                changeYear: true,
                dateFormat: 'yy/mm/dd',
                maxDate: '-1',
                onSelect: function (selected) {
                    $(tu).datepicker("option", "minDate", selected)
                }
            });
            $(tu).datepicker({
                dateFormat: 'yy/mm/dd',
                changeMonth: true,
                changeYear: true,
                maxDate: '-1',
                onSelect: function (selected) {
                    $(frm).datepicker("option", "maxDate", selected)
                }

            });
        })(frm, tu);
    }
});
于 2012-05-22T06:43:46.210 回答