简体   繁体   English

JQuery 没有找到textarea值

[英]JQuery does not find textarea value

I have a textarea in my HTML file, and I want to send its content to the console.我的 HTML 文件中有一个文本区域,我想将其内容发送到控制台。 Here the code:这里的代码:

<p class="i_link">Link:</p>
<textarea name="link" id="Story_Link" cols="100" rows="1"></textarea><br>
<!--!name-->
<p class="i_name">Name:</p>
<textarea name="name" id="Story_Name" cols="50" rows="1"></textarea><br>
<!--!number-->
<p class="i_number">Number:</p>
<textarea name="number" id="Story_Number" cols="1" rows="1"></textarea><br>
<!--!author-->
<p class="i_author">Author:</p>
<textarea name="author" id="Story_Author" cols="50" rows="1"></textarea><br>
<button onclick="myFunction()">Click me</button>
​
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
​
<script>
function myFunction() {
   console.log('hello ' + $('i_link').val())
}
</script>

But it prints out hello undefined to the console.但是它向控制台打印出hello undefined I expected to get the value of my textarea instead of undefined.我希望得到我的 textarea 的值而不是未定义的。

How to get the expected output?如何得到预期的output?

Replace your line更换您的线路

console.log('hello ' + $('i_link').val())

with

console.log('hello ' + $('#Story_Link').val())

You tried to get the value from a <p> tag and you missed the .您试图从<p>标记中获取值,但您错过了. class selector. class 选择器。

My solution will lookup for your Story_Link textarea I used the hash sign for finding that textarea by id.我的解决方案将查找您的 Story_Link textarea 我使用 hash 标志通过 id 查找该 textarea。

Here you will find more information about how to find elements in HTML DOM with jquery:在这里,您将找到有关如何使用 jquery 在 HTML DOM 中查找元素的更多信息:


Here I got you a working example:在这里,我为您提供了一个工作示例:

 function myFunction() { console.log('hello ' + $('#Story_Link').val()) }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p class="i_link">Link:</p> <textarea name="link" id="Story_Link" cols="100" rows="1"></textarea><br> <:--:name--> <p class="i_name">Name:</p> <textarea name="name" id="Story_Name" cols="50" rows="1"></textarea><br> <!--!number--> <p class="i_number">Number:</p> <textarea name="number" id="Story_Number" cols="1" rows="1"></textarea><br> <!--!author--> <p class="i_author">Author:</p> <textarea name="author" id="Story_Author" cols="50" rows="1"></textarea><br> <button onclick="myFunction()">Click me</button>

Your issue is that you haven't supplied a CSS selector to the jQuery object that matches an element and so it's not finding any element to get the value of.您的问题是您没有向 jQuery object 提供与元素匹配的 CSS 选择器,因此它找不到任何元素来获取其值。 If you are trying to get the Author name, you need to pass #Story_Author because that is the id of the textarea where that data is entered.如果您尝试获取作者姓名,则需要传递#Story_Author ,因为这是输入数据的textareaid

 <p class="i_link">Link:</p> <textarea name="link" id="Story_Link" cols="100" rows="1"></textarea><br> <:--:name--> <p class="i_name">Name:</p> <textarea name="name" id="Story_Name" cols="50" rows="1"></textarea><br> <:--.number--> <p class="i_number">Number.</p> <textarea name="number" id="Story_Number" cols="1" rows="1"></textarea><br> <.--.author--> <p class="i_author">Author.</p> <textarea name="author" id="Story_Author" cols="50" rows="1"></textarea><br> <button onclick="myFunction()">Click me</button> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script> function myFunction() { console.log('hello ' + $('#Story_Author').val()) } </script>

But, you have quite a few issues beyond that.但是,除此之外,您还有很多问题。

jQuery is a powerful JavaScript library that can make doing very complex things much simpler. jQuery 是一个功能强大的 JavaScript 库,可以使非常复杂的事情变得更加简单。 When it was first introduced, the core JavaScript language as well as the DOM API weren't as mature as they are now and so even relatively simple queries in "vanilla" JavaScript were cumbersome.首次引入时,核心 JavaScript 语言以及 DOM API 并不像现在这样成熟,因此即使是“vanilla”JavaScript 中相对简单的查询也很麻烦。 That's not the case any longer and, as such, the need for jQuery to do these trivial tasks is gone.情况不再如此,因此,不再需要 jQuery 来完成这些琐碎的任务。 For what you are doing, jQuery is only making things more complex.对于您正在做的事情,jQuery 只会让事情变得更复杂。 Here's all you need to do this without jQuery:在没有 jQuery 的情况下,您只需执行此操作即可:

 <p class="i_link">Link:<br> <input id="Story_Link"></p> <p class="i_name">Name:<br> <input id="Story_Name"></p> <p class="i_number">Number:<br> <input id="Story_Number"></p> <p class="i_author">Author:<br> <input id="Story_Author"></p> <button>Click me</button> <script> // Get references to any elements you'll reference more than once, just once const link = document.getElementById("Story_Link"); const name = document.getElementById("Story_Name"); const number = document.getElementById("Story_Number"); const author = document.getElementById("Story_Author"); // Do your event binding in JavaScript, not HTML document.querySelector("button").addEventListener("click", function(){ console.log('Hello ' + author.value + " Here are your details:"); console.log("\tStory Link: " + link.value); console.log("\tStory Name: " + name.value); console.log("\tStory Number: " + number.value); }); </script>

As you'll see from above, the HTML is much simpler, there's no need to reference jQuery, and the pure JavaScript is also very simple.从上面可以看出,HTML就简单多了,不用引用jQuery,纯JavaScript也很简单。

Additionally:此外:

  • A <textarea> is used for multi-line input. <textarea>用于多行输入。 When you just have a short string to collect, <input> is the correct element to use.当您只有一个短字符串要收集时, <input>是要使用的正确元素。
  • type="text/javascript" has not been needed in script elements for quite a few years now. script元素中已经不需要type="text/javascript"好几年了。
  • You really shouldn't end your paragraphs and then have related content below them that uses a break to separate itself from the next paragraph.你真的不应该结束你的段落,然后在它们下面有相关的内容,这些内容使用中断将自己与下一段分开。 Paragraphs are for complete thoughts.段落是为了完整的想法。 See the updated HTML in the above answer.请参阅上述答案中更新的 HTML。

Change your script code to following将您的脚本代码更改为以下

<script>
function myFunction() {
   console.log('hello ' + $('#Story_Author').val())
}
</script>

notice that.请注意。 in $('._link) while using jquery you have to pass selector of class or id for class you have to add.$('._link)中使用 jquery 时,您必须传递 class 的选择器或 class 的 id,您必须添加。 at start and for id you have to add # at the start在开始和 id 你必须在开始时添加 #

example: to select element with id myId示例:到 select id 为 myId 的元素

$('#myId')

to select element with class myClass到 select 元素与 class myClass

$('.myClass')

Also, the p tag in HTML does not have a value attached to it if you want to get value in the textarea you should select the id of the testarea此外,HTML 中的 p 标签没有附加值,如果你想在 textarea 中获取值,你应该 select testarea 的 id

You want to get value/text from textarea, so you have to write:你想从 textarea 获取值/文本,所以你必须写:

$('#Story_Link').val()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM