简体   繁体   English

如何使用HTML表单将数据发送到javascript以另存为变量

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

I want to use html and javascript (strictly no php) to have a html data entry form to pass data to js to be saved as a variable. 我想使用html和javascript(严格禁止使用php)具有html数据输入表单,以将数据传递给js,以另存为变量。 Any help would be appreciated :) 任何帮助,将不胜感激 :)

This is what a have so far: 到目前为止,这是什么:

        <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>

and then the js side 然后是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
    }

I'm pretty sure the second line of the function is incorrect. 我很确定该函数的第二行不正确。
What I'm looking for in that function is for the data entered into the form on the html page to be saved as the name variable. 我在该函数中寻找的是将输入到html页面上的表单中的数据另存为name变量。

(EDITED) (编辑)

HTML : HTML:

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

JS : JS:

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

maybe it's what you're looking for: 也许这就是您要寻找的:

<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>

See this demo 看这个演示

您不需要提交表单,只需访问您的姓名值即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM