使用JQuery从文本框中删除不需要的字符

编程入门 行业动态 更新时间:2024-10-12 16:30:20
本文介绍了使用JQuery从文本框中删除不需要的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想得到的一些信息是如何使用JQuery从文本框(或textarea)中删除某些字符。我有C#中的代码,但我似乎无法将其转换为JQuery javascript。我的问题是我不知道如何从文本框中获取值作为字符数组,然后我可以循环并与给定的一组不需要的字符进行比较。 这就是我在JQuery中的远:

What I would like to get some input on is how to remove certain characters from a textbox (or textarea) with JQuery. I have the code in C# but I can´t seem to translate that to JQuery javascript. My problem is that I don´t know how to get the value from a textbox as a character array which I then can loop through and compare against a given set of unwanted characters. This is how "far" I have come in JQuery:

$("input[type=text], textarea").change(function() { // code here });

这是我在C#中的代码:

This is my code in C#:

for (int i = 0; i < charArray.Length; i++) { current = charArray[i]; if ((current == 0x9) || (current == 0xA) || (current == 0xD) || ((current >= 0x20) && (current <= 0xD7FF)) || ((current >= 0xE000) && (current <= 0xFFFD))) _validXML.Append(current); } return _validXML.ToString().TrimEnd((char)32, (char)160) ;

更新:

我在下面给出了一些答案的组合(我将赞成它们),我的最终JQuery看起来像这样并起作用:

I went with a combination of some answers below (I will upvote them) and my final JQuery looks like this and works:

$(document).ready(function() { $(":text, textarea").change(function() { var text = ""; var arr = $(this).val() $.each(arr, function(i) { var c = arr.charCodeAt(i); if ((c == 0x9) || (c == 0xA) || (c == 0xD) || (c >= 0x20 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD)) { text += arr.charAt(i); } }); $(this).val(text); }); });

全部谢谢!

推荐答案

Textarea:

<textarea id="item" name="item" rows="5" cols="80">Some text in here</textarea>

jQuery代码:

var text = $('#item').val(); var newtext = ""; for (var i = 0; i < text.length; i++) { var c = text.charCodeAt(i); if ((c == 0x9) || (c == 0xA) || (c == 0xD) || (c >= 0x20 && c <= 0xD7FF) || (c >= 0xE000 && c <= 0xFFFD)) { newtext += c; } } $('#item').val(newtext);

除了访问文本数据并设置它之外,这实际上与jQuery很少有关系。再次。

This has actually very little to do with jQuery, methinks, except to access the text data and set it again.

更多推荐

使用JQuery从文本框中删除不需要的字符

本文发布于:2023-11-02 00:30:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550854.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不需要   框中   字符   文本   JQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!