![](/img/trans.png)
[英]Passing a function on the content of a HTML textarea in Javascript
[英]Send textarea content to javascript function
好的,我得到了一个textarea和一个链接(用作按钮),而我想做的是,当我单击链接时,它将内容从textarea发送到javascript函数。 像这样...:
<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>
只需为您的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>
或者,您可以改为更改myFunction
函数,以使其定义函数内部的值:
JS:
function myFunction() {
var value = document.getElementById('myTextarea').value;
//rest of the code
}
HTML:
<textarea name="text" placeholder="Type your text here!" id="myTextarea"></textarea>
<a href="" onclick="myFunction(); return false;">Send!</a>
而且,如果您使用的是jQuery(确实如此),则可以将document.getElementById('myTextarea').value
更改$('#myTextarea').val();
得到以下内容:
JS:
function myFunction() {
var value = $('#myTextarea').val();
//rest of the code
}
HTML:
<textarea name="text" placeholder="Type your text here!" id="myTextarea"></textarea>
<a href="" onclick="myFunction(); return false;">Send!</a>
您可以像这样简单地实现它,只需调用函数并在该函数中完成所有工作:
<a href="" onclick="myFunction();">Send</a>
jQuery代码:
function myFunction()
{
var text = $('textarea[name="text"]').val();
// use text here
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.