将字符串传递给javascript函数会产生值

时间:2012-06-28 04:24:35

标签: javascript html

这里发生了一些奇怪的事情,我绕圈子试图弄清楚发生了什么......我有几个输入框,有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恰好是输入框的值。

它如何抓住这个价值?如何通过真正的?

2 个答案:

答案 0 :(得分:0)

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

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

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
}

答案 1 :(得分: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);

为什么以及在哪里改变附加到&lt;的函数的传递值? INPUT&gt; ?