簡體   English   中英

如何使用HTML表單將數據發送到javascript以另存為變量

[英]How to use a html form to send data to javascript to be saved as a variable

我想使用html和javascript(嚴格禁止使用php)具有html數據輸入表單,以將數據傳遞給js,以另存為變量。 任何幫助,將不勝感激 :)

到目前為止,這是什么:

        <form id="myform"> //form name
            What is your name: <input type='text' name='name'> //name input
            <a href="javascript: formCatch(name)">Go</a> // running some kind of function?
        </form>

然后是js端

var name = 'test';   

function formCatch() { //declare function 'formCatch'
        name = document.forms["myform"].submit();  // set the name variable as the contents from the form
        alert('hello, ' + name); // alert to test the output
    }

我很確定該函數的第二行不正確。
我在該函數中尋找的是將輸入到html頁面上的表單中的數據另存為name變量。

(編輯)

HTML:

What is your name: <input type='text' id='name'/> 
            <a onclick="formCatch();" href="#">Go</a> 

JS:

function formCatch() { 
        name = getElementById("name").value;
        alert('hello, ' + name);
    }

也許這就是您要尋找的:

<script>
    var name = 'test';   

    function formCatch() {
        document.forms["myform"].submit();
        name = document.getElementById("name").value;
        alert('hello, ' + name);
    }
</script>

<form id="myform">
    What is your name: <input id="name" type='text' name='name'>
    <a href="javascript:formCatch();">Go</a>
</form>

看這個演示

您不需要提交表單,只需訪問您的姓名值即可。

name = document.forms["myForm"]["name"].value;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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