使用 jQuery 获取 Textarea 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5708814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:41:26  来源:igfitidea点击:

Getting Textarea Value with jQuery

jquery

提问by Dave Kiss

This is driving me crazy - why doesn't my code work?

这让我发疯 - 为什么我的代码不起作用?

<a id="send-thoughts" href="">Click</a>
<textarea id="#message"></textarea>

            jQuery("a#send-thoughts").click(function() {
                var thought = jQuery("textarea#message").val();
                alert(thought);
            });

alerts undefined.

警报未定义。

http://jsfiddle.net/q5EXG/

http://jsfiddle.net/q5EXG/

采纳答案by brad

you have id="#message"... should be id="message"

你有id="#message"......应该是id="message"

http://jsfiddle.net/q5EXG/1/

http://jsfiddle.net/q5EXG/1/

回答by nunjimmimya

By using new version of jquery (1.8.2), I amend the current code like in this links http://jsfiddle.net/q5EXG/97/

通过使用新版本的 jquery (1.8.2),我修改当前的代码,如这个链接 http://jsfiddle.net/q5EXG/97/

By using the same code, I just change from jQuery to '$'

通过使用相同的代码,我只是从 jQuery 更改为 '$'

<a id="send-thoughts" href="">Click</a>
<textarea id="message"></textarea>

$('#send-thoughts').click(function()
{ var thought = $('#message').val();
  alert(thought);
});

回答by nunjimmimya

It can be done at easily like as:

它可以很容易地完成,如:

     <a id="send-thoughts" href="">Click</a>
     <textarea id="message"></textarea>

        $("a#send-thoughts").click(function() {
            var thought = $("#message").val();
            alert(thought);
        });

回答by chitcharonko

change id="#message" to id="message" on your textarea element.

将 textarea 元素上的 id="#message" 更改为 id="message"。

and by the way, just use this:

顺便说一句,只需使用这个:

$('#send-thoughts')

remember that you should only use ID's once and you can use classes over and over.

请记住,您应该只使用 ID 一次,并且可以反复使用类。

https://css-tricks.com/the-difference-between-id-and-class/

https://css-tricks.com/the-difference-between-id-and-class/

回答by Vismari

try this:

尝试这个:

<a id="send-thoughts" href="">Click</a>
<textarea id="message"></textarea>
<!--<textarea id="#message"></textarea>-->

            jQuery("a#send-thoughts").click(function() {
                //var thought = jQuery("textarea#message").val();
                var thought = $("#message").val();
                alert(thought);
            });