[英]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.