Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jQuery在用户定义函数中传递控件名_Jquery - Fatal编程技术网

如何使用jQuery在用户定义函数中传递控件名

如何使用jQuery在用户定义函数中传递控件名,jquery,Jquery,我在一个表单中有两个控件,月日和月。无论用户何时更改月份,我都希望它根据月份更改“月中日”组合框中的天数。我想写一个用户定义的函数,因为我在许多不同的页面上有相同的逻辑。 有人知道怎么做吗?在页面加载中,您可以执行以下操作 ddl1.attribute.add("onchange", string.Format("functionName('{0}')", ddl2.ClientId);); 功能日月(月,年){ 返回新日期(年、月、0)。getDate(); } 函数重建OfMonthCon

我在一个表单中有两个控件,月日和月。无论用户何时更改月份,我都希望它根据月份更改“月中日”组合框中的天数。我想写一个用户定义的函数,因为我在许多不同的页面上有相同的逻辑。
有人知道怎么做吗?

在页面加载中,您可以执行以下操作

ddl1.attribute.add("onchange", string.Format("functionName('{0}')", ddl2.ClientId););
功能日月(月,年){
返回新日期(年、月、0)。getDate();
}
函数重建OfMonthControl(controlId,月)
{
var currentYear=(新日期).getFullYear();
var天数=daysInMonth(月份,当前年份);
$(controlId).empty();
//重建月份控制的天数

对于(var x=1;x),上面的代码在第一个组合上添加了一个onchange事件,即月份。我们需要将第二个组合的客户机id传递给该事件。
function daysInMonth(month, year) {
    return new Date(year, month, 0).getDate();
}

function rebuildDaysOfMonthControl(controlId, month)
{
    var currentYear = (new Date).getFullYear();
    var days = daysInMonth(month, currentYear);

    $(controlId).empty();

    //rebuild the days of month control
    for (var x = 1; x <= days; x++) {
        $(controlId).append(
            $('<option></option>').val(x).html(x)
        );
    }
}

$(function () {

    $('#monthControlId').live('change', function () {
        var selectedMonth = $(this).val();

        rebuildDaysOfMonthControl('#daysOfMonthControlId', selectedMonth);

    });

});