具有大量参数的Javascript函数

时间:2015-12-26 10:14:55

标签: javascript

我需要调用带有许多参数的javascript函数。有没有办法像下面的代码一样做但没有写av1,av2,... av58?

从1到58的for循环可以完成这项工作。但是在定义函数时语法是怎样的呢?

<html>
<head>
<script>
function gft1(ff1, ff2, av1,av2, av3, av4, av5, av6, av7, av8,  av9,  av10,  av11,  av12,  av13,  av14,  av15,  av16,  av17,    av18,  av19,  av20,  av21,  av22,  av23,  av24,  av25,  av26,  av27,  av28,   av29,  av30,  av31,  av32,  av33,  av34,  av35,  av36,  av37,  av38,  av39,   av40,  av41,  av42,  av43,  av44,  av45,  av46,  av47,  av48,  av49,  av50,   av51,  av52,  av53,  av54,  av55,  av56,  av57,  av58)
{   
var uu=0;

    for ( y=1; y<=58;++ y)
        {
         uu = uu-(-document.getElementById('av'+y).value);
        }

document.getElementById(ff1).value=uu;
document.getElementById(ff2).value=uu;

}
var args = ['ff1','ff2'];
for (var i = 1; i <= 58; i++)
args.push("av" + i);
</script>  

</head>
 ....................................
   <html><input type="text" size="1" autocomplete="off"  onfocus="this.select()" name="<?php echo "av[$no]"?>" id="<?php echo "av{$no}" ? >" onkeyup="gft1.apply(null, args);"></html>

2 个答案:

答案 0 :(得分:7)

你可以使用for循环来填充参数池

然后使用apply来调用它并传递参数

function gft1(){
    console.log(arguments);
}

var args = ['ff1','ff2'];
for (var i = 1; i <= 58; i++)
    args.push("av" + i);

gft1.apply(null, args);

查看apply here

的用法

答案 1 :(得分:0)

通过av1,av2,av3....传递可以简单地完成av时,不确定是否要求传递av1,av2,av3,...。你也没有做任何事情av,你只是处理av&amp;循环从1到58并将y值附加到function gft1('ff1',ff2,'av'){ var uu=0; for ( y=1; y<=58;++ y){ uu = uu-(-document.getElementById('av'+y).value); // ^^ y's value will vary which will query for `av1,av2,av3..` } document.getElementById(ff1).value=uu; document.getElementById(ff2).value=uu; } 以从DOM获取内容。

这样的功能不够吗?

$(document).ready(function () {
    $(".input-search").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '/WebServices/GetAllBrandNames.asmx/getAllBrands',
                data: "{ 'data': '" + request.term + "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    response($.map(data.d, function (item) {
              // don't forget to $.map to (data.d where d is the top node
                        return {
             // assign values from json response for further use
                            brandid: item.BrandId,
                            brand: item.BrandName,
                            maincommodityid: item.MainCommodityId,
                            maincommodity: item.MainCommodityName,
                            subcommodityid: item.SubcommodityId,
                            subcommodity: item.SubCommodityName
                        }
                    }));
                },
                error: function (response) {
                    alert('Server Error. Please try again.');
                },
                failure: function (response) {
                    alert('Failed to get result');
                }
            });
        },
        focus: function (event, ui) {
            // prevent autocomplete from updating the textbox
            event.preventDefault();
        },
        select: function (event, ui) {
            // prevent autocomplete from updating the textbox
            event.preventDefault();
            // do further action here such as assigning id to hidden field etc. 
            $(".input-search").val(ui.item.brand);
            // navigate to the selected item's url ex: /catalogue/1/1/pulses/red-gram/4/president
            var str = "/catalogue/" + ui.item.maincommodityid + "/" +
                 ui.item.subcommodityid + "/" + $.trim(ui.item.maincommodity.replace(/\s+/g, '-').toLowerCase()) + "/" +
                 $.trim(ui.item.subcommodity.replace(/\s+/g, '-').toLowerCase()) + "/" + ui.item.brandid + "/" +
                  $.trim(ui.item.brand.replace(/\s+/g, '-').toLowerCase());
            window.location = str;
        },
        minLength: 3
    }).autocomplete("instance")._renderItem = function (ul, item) {
        // get values and create custom display
        var $a = $("<a></a>");                  
        $("<strong></strong>").text(item.brand).appendTo($a);
        $("<span></span>").text(" in ").appendTo($a);
        $("<span></span>").text(item.subcommodity).appendTo($a);
        return $("<li></li>").append($a).appendTo(ul);
    };
});