【问题标题】:Remove Ordered/Unordered Bullets while copy pasting in text box在文本框中复制粘贴时删除有序/无序项目符号
【发布时间】:2018-08-13 02:56:48
【问题描述】:

在将文本从 MS Word 复制粘贴到 html 文本框时,项目符号会附加在文本框中。如何使用 Jquery/Javascript 删除它?

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 我从 word doc 中复制了列表中的文本,并将其粘贴到 html 文本框中。我使用下面的代码在文本框中粘贴值时只允许使用字母数字。但上述项目符号表示为“o”字符。所以它不能被限制。 $("#CustomerId").on("paste", function (e) { // 使用 api 访问剪贴板 var pastedData = e.originalEvent.clipboardData.getData('text'); $("#CustomerId") .val(pastedData.replace(/[^A-Z0-9]/ig, "")); e.preventDefault(); });
  • 我更新了我的答案以检查“o”后跟空格

标签: javascript jquery html html.textbox


【解决方案1】:

有很多方法可以从文本框中删除不需要的字符。一种方法是使用正则表达式从该值字段中过滤掉任何非字母数字字符,并将过滤后的值放回文本框中,替换/覆盖旧值。

$('#my_textbox').keyup(function() {
  //this code will be triggered after each keystroke inside the textbox
  // alternatively, you may want to only trigger it after blur/change/etc

  var cleaned = $(this).val().replace(/^\s*[o\W]\s+/g, '');
  //removes any letter 'o' followed by spaces
  //removes any non-alphanumeric followed by spaces

  $(this).val(cleaned);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h3>Paste something here with a bullet point</h3>
<input type="text" id="my_textbox">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2012-05-04
    相关资源
    最近更新 更多