我的autocomma只适用于第一个文本输入,但不适用于第二个

时间:2017-03-15 09:33:15

标签: javascript html

自动逗号只适用于第一个,但它不能用于第二个文本输入,请帮帮我

var MoneyInput = {
    value: 0,
    el: null,
    textfield: null,
    max: Number.MAX_VALUE,
    min: 0,


    change: null,


    slider: null,


    create: function(el) {
        return Object.create(this).init(el);
    },


    init: function(el) {
        var _this = this,
            textfield = el.querySelector('.textfield'),
            pattern = /[0-9]/;

        if (el.classList.contains('inited')) return; //do not initialize the wrapper again

        el.classList.add('inited');

        this.min = parseFloat(el.dataset.min) || 0;
        this.max = parseFloat(el.dataset.max) || Number.MAX_VALUE;

        textfield.addEventListener('keypress', function(e) {
            var inputChar = String.fromCharCode(e.charCode);
            // console.log(inputChar, e.charCode);

            if (!pattern.test(inputChar)) {
                //invalid character, prevent input
                e.preventDefault();
            }

        });

        textfield.addEventListener('input', function(/*e*/) {
            _this._inputHandler();
        });

        textfield.addEventListener('click', function(/*e*/) {
            if (!_this.value) {
                textfield.value = '';
            }
        });

        textfield.addEventListener('blur', function(/*e*/) {
            _this._blurHandler();
        });

        this.textfield = textfield;
        this.el = el;
        return this; // for chaining
    },

    _inputHandler: function() {
        var textfield = this.textfield,
            el = this.el,
            str = textfield.value.replace(/,/g, ''),
            invalid = false,
            value = parseFloat(str) || 0;

        if (value < this.min && str) { value = this.min; invalid = true; }
        if (value > this.max) { value = this.max; invalid = true; }
        if (/^0+/.test(str)) {
            //if the text start with zeros
            invalid = true;
        }

        this.value = value;
        this.el.dataset.value = this.value;
        textfield.value = (invalid) ? String.addCommas(value) : String.addCommas(str);
        // console.log(textfield.value, this.value);
        //apply validation highlight for .money-input
        if (this.value) {
            el.classList.add('validated');
        } else {
            el.classList.remove('validated');
        }

        if (typeof this.change === 'function') this.change(this.value);
        if (this.slider && this.slider.setValue) this.slider.setValue(this.value);
    },

    _blurHandler: function() {
        //normalize number input (when there are more . in the strings)
        if (this.value) {
            this.setValue(this.value);
        } else {
            this.el.value = '';
        }
    },

    setValue: function(value) {
        if (isNaN(value)) value = 0;
        this.value = value;
        this.strValue = String.addCommas(value);
        this.textfield.value = this.strValue;
        this.el.dataset.value = this.value;
        if(this.value) {
            this.el.classList.add('validated');
        } else {
            this.el.classList.remove('validated');
        }
        // if(typeof this.change === 'function') this.change(this.value);
    },

    getValue: function() {
        return this.value;
    }
};


window.MoneyInput = MoneyInput;
})();

MoneyInput.create(document.querySelector('#my-input'));

js文件 / *©2013 aleph-labs.com  * @author Thanh Tran  * /       (function(){

/ *      *将逗号添加到数字中      * @方法      * @param {*} nStr Numer / String添加逗号      * @param {String} sep自定义分隔符代替逗号      * @return {String}添加逗号的字符串      * @return String添加了逗号的数字字符串      * @静态的      * @author未知      * /     String.addCommas = function(nStr,sep){         if(nStr === undefined)nStr ='';         var parts = nStr.toString()。split('。');         if(!sep)sep =',';         parts [0] = parts [0] .replace(/ \ B(?=(\ d {3})+(?!\ d))/ g,sep);         return parts.join('。');     };

SELECT  *
FROM    A 
JOIN    B 
ON      A.ref_track = B.id
JOIN    C
ON      C.id = B.ref_plannode
WHERE   B.id= 1
        AND A.`number.client` > 50
ORDER BY A.id DESC

1 个答案:

答案 0 :(得分:0)

使用querySelectorAll()代替 - 您当前的querySelector()仅定位找到的第一个元素。查看https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll