2

我想对输入使用掩码,SP并以PT. 输入PT工作正常,但SP没有。请注意,这不是我的真实代码,它只是一个示例。

$('#partshort').mask('SP-00000000000');
$('#svpshort').mask('PT-00000000000');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="http://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<div class="container">
  <h3>Problems with jQuery Mask Plugin?</h3>
  <p>Exemplify your problem here :-)</p>
  <hr />
  <form role="form">
    <div class="form-group">
      <label for="date-field">Example 1</label>
      <input type="text" class="form-control" id="partshort">
    </div>
    <div class="form-group">
      <label for="ip-field">Example 2</label>
      <input type="text" class="form-control" id="svpshort">
    </div>
  </form>
</div>

这是示例:http: //jsfiddle.net/1wjbysnm/

4

1 回答 1

2

问题是因为SMask 库中的字符具有特殊含义:

默认情况下,jQuery Mask Plugin 仅识别逻辑数字 A(数字和字母)和 S(A-Za-z),但您可以扩展或修改此行为

https://igorescobar.github.io/jQuery-Mask-Plugin/docs.html#translation

要解决您的问题,您可以简单地删除S字符翻译,使其表现为任何标准字符:

$('#partshort').mask('SP-00000000000', {
  translation: { S: null }
});
$('#svpshort').mask('PT-00000000000');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="http://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<div class="container">
  <h3>Problems with jQuery Mask Plugin?</h3>
  <p>Exemplify your problem here :-)</p>
  <hr />
  <form role="form">
    <div class="form-group">
      <label for="date-field">Example 1</label>
      <input type="text" class="form-control" id="partshort">
    </div>
    <div class="form-group">
      <label for="ip-field">Example 2</label>
      <input type="text" class="form-control" id="svpshort">
    </div>
  </form>
</div>

于 2019-02-11T16:03:06.943 回答