无论如何要屏蔽输入字段?

我有一个输入字段,我想对其应用自动斜线。类似于 MM/YY。

例如,如果有人输入 MM (05),它应该自动添加一个斜线来分隔月份和年份,如果有人删除第二个数字,则应该删除斜线。

是否可以通过 CSS 实现?有人可以帮助我如何做吗?


<input type="number">


料青山看我应如是
浏览 99回答 2
2回答

繁花不似锦

我不建议你使用这种技术。而不是这些使用一些外部库$("#masked").on("change paste keyup", function() {&nbsp; let inputValue = $(this).val();&nbsp; if (inputValue.split("/")[0] > 12) {&nbsp; &nbsp; $(this).val(12);&nbsp; }});$("#masked").mask("00/00");<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="http://igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js"></script><div id="app">&nbsp; <h3>Masked Input Plugin as MM/YY.</h3>&nbsp; <table border="0">&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input id="masked" type="text" value="" placeholder="MM/YY" data-mask="00/00"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data-mask-clearifnotmatch="true"/>&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <td>typing 0221 but getting 02/21</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </tbody>&nbsp; </table></div>

三国纷争

<!DOCTYPE HTML><html><form action="">&nbsp; <div>&nbsp; &nbsp; <label for="cc">Expiration Date</label>&nbsp; &nbsp; <!-- Set via HTML -->&nbsp; &nbsp; <input id="cc" type="text" placeholder="MM/YY" class="masked" pattern="(1[0-2]|0[1-9])\/(1[5-9]|2\d)" data-valid-example="05/18"/>&nbsp; </div></form><script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/masking-input.js" data-autoinit="true"></script></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript