[英]Send textarea content to javascript function
Ok so I got a textarea and a link (which works as a button) and what I am trying to do is that when I click on the link, it sends the content from the textarea to a javascript function. 好的,我得到了一个textarea和一个链接(用作按钮),而我想做的是,当我单击链接时,它将内容从textarea发送到javascript函数。 Something like this...:
像这样...:
<textarea name="text" placeholder="Type your text here!"></textarea>
<a href="" onclick="myFunction(<!-- sends the value of the textarea that the user enters and then sends it to myFunction -->); return false;">Send!</a>
Just assign an id
to your textarea, and use document.getElementById
: 只需为您的textarea分配一个
id
,然后使用document.getElementById
:
<textarea name="text" placeholder="Type your text here!" id="myTextarea"></textarea>
<a href="" onclick="myFunction(document.getElementById('myTextarea').value); return false;">Send!</a>
Alternatively, you could instead change your myFunction
function, to make it define the value inside the function: 或者,您可以改为更改
myFunction
函数,以使其定义函数内部的值:
JS: JS:
function myFunction() {
var value = document.getElementById('myTextarea').value;
//rest of the code
}
HTML: HTML:
<textarea name="text" placeholder="Type your text here!" id="myTextarea"></textarea>
<a href="" onclick="myFunction(); return false;">Send!</a>
And if you're using jQuery, which it does look like you do, you could change the document.getElementById('myTextarea').value
to $('#myTextarea').val();
而且,如果您使用的是jQuery(确实如此),则可以将
document.getElementById('myTextarea').value
更改$('#myTextarea').val();
to get the following: 得到以下内容:
JS: JS:
function myFunction() {
var value = $('#myTextarea').val();
//rest of the code
}
HTML: HTML:
<textarea name="text" placeholder="Type your text here!" id="myTextarea"></textarea>
<a href="" onclick="myFunction(); return false;">Send!</a>
you can do simply like this to achieve it, just call function and do all work in that function: 您可以像这样简单地实现它,只需调用函数并在该函数中完成所有工作:
<a href="" onclick="myFunction();">Send</a>
Jquery code: jQuery代码:
function myFunction()
{
var text = $('textarea[name="text"]').val();
// use text here
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.