簡體   English   中英

將textarea內容發送到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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM