Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.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
Javascript 将数组值传递给函数_Javascript_Jquery_Arrays_For Loop - Fatal编程技术网

Javascript 将数组值传递给函数

Javascript 将数组值传递给函数,javascript,jquery,arrays,for-loop,Javascript,Jquery,Arrays,For Loop,在下面的脚本中,console.log返回正确的值,两个警报都会触发。但是,该值未定义 知道我做错了什么吗 jQuery(document).ready(function(){ jQuery("#customfield_21070").attr('style','width:60px'); jQuery("#customfield_21070").attr('disabled','disabled'); var customfields = [ '#custo

在下面的脚本中,console.log返回正确的值,两个警报都会触发。但是,该值未定义

知道我做错了什么吗

jQuery(document).ready(function(){

    jQuery("#customfield_21070").attr('style','width:60px');
    jQuery("#customfield_21070").attr('disabled','disabled');

    var customfields = [
    '#customfield_11070',
    '#customfield_11071',
    '#customfield_20071',
    '#customfield_20072',   
    '#customfield_20073',
    '#customfield_20074',
    ];

    for(var i=0, len=customfields.length; i<len; i++) {
        console.log(customfields[i]);
        jQuery(customfields[i]).keyup(function(){
            alert('calculate');//FIRES
            calculateSum();
        });
    }


    function calculateSum() {

        var sum = 0;

        alert('value: ' + this.value);//UNDEFINED

        if(!isNaN(this.value) && this.value.length!=0 && this.id !== "customfield_21070") {
            sum += parseFloat(this.value);
        }

        jQuery("#customfield_21070").val(sum.toFixed(2));
    }

});
jQuery(文档).ready(函数(){
jQuery(#customfield_21070”).attr('style','width:60px');
jQuery(#customfield_21070”).attr('disabled','disabled');
var自定义字段=[
“#customfield_11070”,
“#customfield_11071”,
"#customfield_20071",,
“#customfield_20072”,
"#customfield_20073",,
“#customfield_20074”,
];
对于(var i=0,len=customfields.length;i使用:

使用给定的
值和提供的参数调用函数
个别地

它的第一个参数是
thisArg

的值是为调用fun提供的。请注意,
可能不会 是该方法看到的实际值:如果该方法是 非严格模式代码,null和undefined将替换为 全局对象和基元值将被装箱

因此,将
这个
从处理函数传递到
calculateSum
,如下所示:

jQuery(customfields[i]).keyup(function(){
    calculateSum.call(this);
});

@canon
是绝对正确的,正如他所说的那样,以下内容将解决您的问题

你可以写:-

jQuery(document).ready(function () {

            jQuery("#customfield_21070").attr('style', 'width:60px');
            jQuery("#customfield_21070").attr('disabled', 'disabled');

            var customfields = [
            '#customfield_11070',
            '#customfield_11071',
            '#customfield_20071',
            '#customfield_20072',
            '#customfield_20073',
            '#customfield_20074',
            ];

            for (var i = 0, len = customfields.length; i < len; i++) {
                jQuery(customfields[i]).keyup(function () {
                    calculateSum(this);//Edited
                });
            }


            function calculateSum(param) {//Edited

                var sum = 0;

                alert('value: ' + param.value);//UNDEFINED

                if (!isNaN(param.value) && param.value.length != 0 && param.id !== "customfield_21070") {//Edited
                    sum += parseFloat(param.value);
                }

                jQuery("#customfield_21070").val(sum.toFixed(2));
            }

        });
jQuery(文档).ready(函数(){
jQuery(#customfield_21070”).attr('style','width:60px');
jQuery(#customfield_21070”).attr('disabled','disabled');
var自定义字段=[
“#customfield_11070”,
“#customfield_11071”,
"#customfield_20071",,
“#customfield_20072”,
"#customfield_20073",,
“#customfield_20074”,
];
对于(变量i=0,len=customfields.length;i

这反映了
警报中的正确值
**Tested**
未引用
calculateSum
中的键控元素
在当前上下文中它在
calculateSum
函数中没有调用任何内容。