Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

javascript - jQuery获取textarea文本(jQuery get textarea text)

Recently I have started playing with jQuery, and have been following a couple of tutorials.

(最近,我开始使用jQuery,并且已经关注了一些教程。)

Now I feel slightly competent with using it (it's pretty easy), and I thought it would be cool if I were able to make a 'console' on my webpage (as in, you press the ` key like you do in FPS games, etc.), and then have it Ajax itself back to the server in-order to do stuff.

(现在,我觉得使用它有点能力(这很容易),而且我认为如果能够在网页上制作一个“控制台”(例如,像在FPS游戏中一样按下“键”,等),然后将Ajax本身返回到服务器以执行操作。)

I originally thought the best way would be to just get the text inside the textarea, and then split it, or should I use the keyup event, convert the keycode returned to an ASCII character, append the character to a string and send the string to the server (then empty the string).

(我本来以为最好的方法是将文本放入文本区域内,然后将其拆分,或者我应该使用keyup事件,将返回的键码转换为ASCII字符,将该字符附加到字符串中并将该字符串发送到服务器(然后清空字符串)。)

I couldn't find any information on getting text from a textarea, all I got was keyup information.

(我找不到有关从文本区域获取文本的任何信息,我所得到的只是键盘信息。)

Also, how can I convert the keycode returned to an ASCII character?

(另外,如何将返回的键码转换为ASCII字符?)

  ask by RodgerB translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why would you want to convert key strokes to text?

(您为什么要将击键转换为文本?)

Add a button that sends the text inside the textarea to the server when clicked.

(添加一个按钮,单击该按钮可将文本区域内的文本发送到服务器。)

You can get the text using the value attribute as the poster before has pointed out, or using jQuery's API:

(您可以使用value属性作为海报之前指出的内容,或使用jQuery的API:)

$('input#mybutton').click(function() {
    var text = $('textarea#mytextarea').val();
    //send to server and process response
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...