【问题标题】:jQuery plugin not working with Aurelia in Firefox onlyjQuery 插件仅在 Firefox 中无法与 Aurelia 一起使用
【发布时间】:2016-11-17 07:54:52
【问题描述】:

我已经启动了一个 Aurelia 项目,并选择使用 this plugin 来启用电话号码的输入屏蔽,使用类似于 this post 中的实现。

它在 Chrome 和 Safari 中运行良好 - 但是,它在 Firefox 中无法正常运行。没有错误或其他有用的信息。但是,上面链接的演示页面上的示例工作得很好,所以我确信它一定与我的实现有关:

JS:

import {inject, NewInstance} from 'aurelia-dependency-injection';
import {ValidationController, ValidationRules, validateTrigger} from 'aurelia-validation';
import 'igorescobar/jQuery-Mask-Plugin'

@inject(NewInstance.of(ValidationController))

export class MyForm {
  async activate(params, routeConfig) {
    // do stuff if there are route parameters
  }

  bind() {
    $('#PhoneNumber').mask('(000) 000-0000');
  }

  constructor(controller) {
    this.controller = controller;
    this.controller.validateTrigger = validateTrigger.manual;
  }

  submit() {
    this.controller.validate()
      .then(errors => {
        if (errors.length === 0) {
          // do a thing
        } else {
          // do something else
        }
      });
  }
}

ValidationRules
  .ensure('phoneNumber').displayName('Phone number')
  .minLength(10).withMessage('Phone number must be at least 10 characters')
  .maxLength(14).withMessage('Phone number is too long')
  .matches(/[(][0-9]{3}[)] [0-9]{3}-[0-9]{4}/).withMessage('Please provide a valid phone number')
  .on(MyForm);

HTML

<input class="form-control" id="PhoneNumber" type="tel" minlength="10" maxlength="14" pattern="[(][0-9]{3}[)] [0-9]{3}-[0-9]{4}" value.bind="phoneNumber & validate" required="required">

我尝试删除模式属性并将其更改为常规文本输入,但无济于事。我真的在这个问题上摸不着头脑。任何想法或建议将不胜感激。

【问题讨论】:

    标签: jquery firefox jquery-plugins aurelia jspm


    【解决方案1】:

    我和 Jan 一起工作 - 我们从来没有让它在 Firefox 中像原始问题的编码方式那样工作。

    我们最终还是用 jquery.inputmask 替换了它,因为我们遇到了 jquery.mask('igorescobar/jQuery-Mask-Plugin') 的兼容性问题。

    这是使它起作用的胶水(基于Two-Way binding in an Aurelia Custom Attribute):

    input-mask.js

    import {inject, customAttribute, bindable, bindingMode} from 'aurelia-framework';
    import 'jquery.inputmask';
    import 'jquery.inputmask.numeric';
    
    @customAttribute('input-mask')
    @inject(Element)
    export class InputMaskCustomAttribute {
    
      @bindable({defaultBindingMode: bindingMode.twoWay}) unmaskedValue;
    
      @bindable maskOptions;
    
      element;
    
      isValidInputElement;
      // The maskedinput jquery pluggin does not allow the events that aurelia needs
      // for binding to get through.  So we will manually put them through.
      // This is the list of events we are going to look for.  "focusout" allows for the value
      // to be correct intime for "onblur".
      aureliaBindEvents = 'focusout';
      eventTarget = undefined;
    
      constructor(element) {
        if (element instanceof HTMLInputElement) {
          this.element = element;
          this.isValidInputElement = true;
          this.eventTarget = $(this.element);
        } else {
          this.isValidInputElement = false;
        }
      }
    
      bind() {
        this.element.value = this.unmaskedValue;
      }
    
      attached() {
        this.setupMask();
      }
    
      detached() {
        this.tearDownMask();
      }
    
      maskOptionsChanged(newValue, oldValue) {
        this.tearDownMask();
        this.setupMask();
      }
    
      setupMask(mask) {
        if (this.isValidInputElement && this.maskOptions) {
          this.eventTarget.inputmask(this.maskOptions);
          this.eventTarget.on(this.aureliaBindEvents, (e) => {
            this.unmaskedValue = this.eventTarget.inputmask('unmaskedvalue');
            this.fireEvent(e.target, 'input');
          });
        }
      }
    
      tearDownMask() {
        if (this.isValidInputElement) {
          if (this.eventTarget) {
            this.eventTarget.off(this.aureliaBindEvents);
            this.eventTarget.inputmask('remove');
          }
        }
      }
    
      createEvent(name: string) {
        let event = document.createEvent('Event');
        event.initEvent(name, true, true);
        return event;
      }
    
      fireEvent(element: HTMLElement, name: string) {
        var event = this.createEvent(name);
        element.dispatchEvent(event);
      }
    
    }
    

    widget.html

    <require from="input-mask"></require>
    <input class="form-control" type="text" maxlength="12" 
      input-mask="mask-options.bind: {alias: 'currency', rightAlign: false, allowMinus:false, allowPlus: false}; unmasked-value.bind: target['monthly-payment'] & validate" />
    

    【讨论】:

    • 我不明白。如果我复制您的代码,那么当我将鼠标悬停(或聚焦)在输入元素上时,会出现 word currency,但输入不可编辑,这意味着它不会响应输入任何值。
    • 我认为这是因为您确实在依赖项管理器中引入了 jquery.inputmask.numeric.js。以下是我使用 JSPM 的方法(在 config.js 中的 'map' 键中):"jquery.inputmask": "npm:jquery.inputmask@3.3.3", "jquery.inputmask.numeric": "npm:jquery.inputmask@3.3.3/dist/inputmask/inputmask.numeric.extensions.js",
    • 我实际上搞砸了别的东西...谢谢你的帮助:)
    【解决方案2】:

    您可以在attached() 事件(在 html 渲染之后)使用 jquery 命令,而不是在bind()。试试看:

    attached() {
      $('#PhoneNumber').mask('(000) 000-0000');
    }
    

    【讨论】:

    • 在 Firefox 中似乎仍然不起作用(不过 Chrome 很好)。还有其他想法吗?
    • 在 Firefox 中按“CTRL+SHIFT+J”可在 javascript 控制台中查看所有错误日志以获取有关问题的更多信息。你能在那儿看到任何嫌疑人吗?
    • 标准 Aurelia 开发调试语句以及 console.log 的输出我放入附加的方法以确保它被触发 - 没有错误。 Chrome 和 FF 之间的调试输出似乎也相同,因此加载似乎没有什么不同。
    猜你喜欢
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多