0

这里发生了一些非常奇怪的事情,我绕着圈子试图弄清楚发生了什么......我有几个输入框,为它们触发了 onchange 事件,该事件被加载了一个 JS 函数,该函数需要值(另一个项目的名称)并相应地操作函数。唯一的问题是,当字符串的值到达另一个函数时,它以某种方式被分配了一个数值,特别是输入框的数值。

我的 php 帮助构建表单:

$filterfield = '"p_delweek"';
print "<span class='filter'>Del Week<input class='menulink spin-button' id='weekno' type='text' value='".$weekno."' onKeyUp='doFilter($filterfield)' onChange='doFilter($filterfield)' data-filtered='0'/><input type='button' value='Clear' onClick='doUnfilter()'></span>";
$filterfield = '"p_seedweek"';
print "<span class='filter'>Sow Week<input class='menulink spin-button' id='sowweekno' type='text' value='".$weekno."' onKeyUp='doFilter($filterfield)' onChange='doFilter($filterfield)' data-filtered='0'/><input type='button' value='Clear' onClick='doUnfilter()'></span>";

在源代码中生成 HTML:

<span class="filter">Del Week<input style="width: 50px; height: 22px;" class="menulink spin-button smartspinner" id="weekno" value="26" onkeyup='doFilter("p_delweek")' onchange='doFilter("p_delweek")' data-filtered="0" type="text"><input value="Clear" onclick="doUnfilter()" type="button"></span><span class="filter">Sow Week<input style="width: 50px; height: 22px;" class="menulink spin-button smartspinner" id="sowweekno" value="26" onkeyup='doFilter("p_seedweek")' onchange='doFilter("p_seedweek")' data-filtered="0" type="text"><input value="Clear" onclick="doUnfilter()" type="button"></span>

被调用的Javascript函数:

function doFilter(filterfield) {
    console.log("DoFilter:"+filterfield);

    var filterInfo=[
        {
            fieldName : filterfield,
            logic : "equal",
            value : Sigma.Util.getValue("weekno")
        }
    ]

    // the next lines action the filtering
    var grid=Sigma.$grid("myGrid1");
    console.log("filterinfo="+filterInfo);
    var rowNOs=grid.applyFilter(filterInfo); 
}

一切都很好,直到我们到达 console.log("DoFilter:"+filterfield) ,结果是 DoFilter:25; 25恰好是输入框的值。

它是如何抓住这个价值的?怎么通过真题?

4

2 回答 2

0

TBH — 我不确定我是否得到了你想要的东西。但是,如果您必须内联调用函数(我建议您不要),您可以将输入字段的引用作为参数传递并使其在方法的主体中可用:

<input onchange="doFilter('p_delweek', this)" type="text">

​</p>

function doFilter(filterfield, field) {
    console.log(filterfield);
    // field is a reference to the input field, hence
    console.log(field.value);
    // will print the current value for this field
}
于 2012-06-28T05:59:01.233 回答
0

这不是答案,这个文件是问题:

        (function($) {
        $.fn.extend({
            spinit: function(options) {
                var settings = $.extend({ min: 0, max: 100, initValue: 0, callback: doFilter, stepInc: 1, pageInc: 10, width: 50, height: 15, btnWidth: 10, mask: '' }, options);
                return this.each(function() {
                    var UP = 38;
                    var DOWN = 40;
                    var PAGEUP = 33;
                    var PAGEDOWN = 34;
                    var mouseCaptured = false;
                    var mouseIn = false;
                    var interval;
                    var direction = 'none';
                    var isPgeInc = false;
                    var value = Math.max(settings.initValue, settings.min);
                    var el = $(this).val(value).css('width', (settings.width) + 'px').css('height', settings.height + 'px').addClass('smartspinner');
                    raiseCallback(value);
                    if (settings.mask != '') el.val(settings.mask);
                    $.fn.reset = function(val) {
                        if (isNaN(val)) val = 0;
                        value = Math.max(val, settings.min);
                        $(this).val(value);
                        raiseCallback(value);
                    };
                    function setDirection(dir) {
                        direction = dir;
                        isPgeInc = false;
                        switch (dir) {
                            case 'up':
                                setClass('up');
                                break;
                            case 'down':
                                setClass('down');
                                break;
                            case 'pup':
                                isPgeInc = true;
                                setClass('up');
                                break;
                            case 'pdown':
                                isPgeInc = true;
                                setClass('down');
                                break;
                            case 'none':
                                setClass('');
                                break;
                        }
                    }
                    el.focusin(function() {
                        el.val(value);
                    });
                    el.click(function(e) {
                        mouseCaptured = true;
                        isPgeInc = false;
                        clearInterval(interval);
                        onValueChange();
                    });
                    el.mouseenter(function(e) {
                        el.val(value);
                    });
                    el.mousemove(function(e) {

                        if (e.pageX > (el.offset().left + settings.width) - settings.btnWidth - 4) {
                            if (e.pageY < el.offset().top + settings.height / 2)
                                setDirection('up');
                            else
                                setDirection('down');
                        }
                        else
                            setDirection('none');
                    });
                    el.mousedown(function(e) {
                        isPgeInc = false;
                        clearInterval(interval);
                        interval = setTimeout(onValueChange, 250);
                    });
                    el.mouseup(function(e) {
                        mouseCaptured = false;
                        isPgeInc = false;
                        clearInterval(interval);

                    });
                    el.mouseleave(function(e) {
                        setDirection('none');
                        if (settings.mask != '') el.val(settings.mask);
                    }); el.keydown(function(e) {
                        switch (e.which) {
                            case UP:
                                setDirection('up');
                                onValueChange();
                                break; // Arrow Up
                            case DOWN:
                                setDirection('down');
                                onValueChange();
                                break; // Arrow Down
                            case PAGEUP:
                                setDirection('pup');
                                onValueChange();
                                break; // Page Up
                            case PAGEDOWN:
                                setDirection('pdown');
                                onValueChange();
                                break; // Page Down
                            default:
                                setDirection('none');
                                break;
                        }
                    });

                    el.keyup(function(e) {
                        setDirection('none');
                    });
                    el.keypress(function(e) {
                        if (el.val() == settings.mask) el.val(value);
                        var sText = getSelectedText();
                        if (sText != '') {
                            sText = el.val().replace(sText, '');
                            el.val(sText);
                        }
                        if (e.which >= 48 && e.which <= 57) {
                            var temp = parseFloat(el.val() + (e.which - 48));
                            if (temp >= settings.min && temp <= settings.max) {
                                value = temp;
                                raiseCallback(value);
                            }
                            else {
                                e.preventDefault();
                            }
                        }
                    });
                    el.blur(function() {
                        if (settings.mask == '') {
                            if (el.val() == '')
                                el.val(settings.min);
                        }
                        else {
                            el.val(settings.mask);
                        }
                    });
                    el.bind("mousewheel", function(e) {
                        if (e.wheelDelta >= 120) {
                            setDirection('down');
                            onValueChange();
                        }
                        else if (e.wheelDelta <= -120) {
                            setDirection('up');
                            onValueChange();
                        }

                        e.preventDefault();
                    });
                    if (this.addEventListener) {
                        this.addEventListener('DOMMouseScroll', function(e) {
                            if (e.detail > 0) {
                                setDirection('down');
                                onValueChange();
                            }
                            else if (e.detail < 0) {
                                setDirection('up');
                                onValueChange();
                            }
                            e.preventDefault();
                        }, false);
                    }

                    function raiseCallback(val) {
                        if (settings.callback != null) settings.callback(val);
                    }
                    function getSelectedText() {

                        var startPos = el.get(0).selectionStart;
                        var endPos = el.get(0).selectionEnd;
                        var doc = document.selection;

                        if (doc && doc.createRange().text.length != 0) {
                            return doc.createRange().text;
                        } else if (!doc && el.val().substring(startPos, endPos).length != 0) {
                            return el.val().substring(startPos, endPos);
                        }
                        return '';
                    }
                    function setValue(a, b) {
                        if (a >= settings.min && a <= settings.max) {
                            value = b;
                        } el.val(value);
                    }
                    function onValueChange() {
                        if (direction == 'up') {
                            value += settings.stepInc;
                            if (value > settings.max) value = settings.max;
                            setValue(parseFloat(el.val()), value);
                        }
                        if (direction == 'down') {
                            value -= settings.stepInc;
                            if (value < settings.min) value = settings.min;
                            setValue(parseFloat(el.val()), value);
                        }
                        if (direction == 'pup') {
                            value += settings.pageInc;
                            if (value > settings.max) value = settings.max;
                            setValue(parseFloat(el.val()), value);
                        }
                        if (direction == 'pdown') {
                            value -= settings.pageInc;
                            if (value < settings.min) value = settings.min;
                            setValue(parseFloat(el.val()), value);
                        }
                        raiseCallback(value);
                    }
                    function setClass(name) {
                        el.removeClass('up').removeClass('down');
                        if (name != '') el.addClass(name);
                    }
                });
            }
        });
    })(jQuery);

为什么以及在哪里改变附加到 <INPUT> 的函数的传递值?

于 2012-06-28T10:35:36.180 回答