0

以下のスクリプトでは、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));
    }

});
4

2 に答える 2

0

@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));
            }

        });

これは、 の適切な値を反映していますalert**Tested**

于 2013-04-18T19:54:59.730 に答える