从 HTML 表单按钮向 javascript 发送多个参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8924232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:01:00  来源:igfitidea点击:

Sending multiple parameters to javascript from a HTML form button

javascripthtmlajaxforms

提问by ragebunny

Ok so i have a form that has three input boxes, at the moment they're text, but they will be changed to password. When the submit button is pressed i want the for to send the form data to an Ajax script.

好的,所以我有一个包含三个输入框的表单,目前它们是文本,但它们将更改为密码。当按下提交按钮时,我希望 for 将表单数据发送到 Ajax 脚本。

I have this working with just one parameter the button looks like this:

我只用一个参数来处理这个按钮看起来像这样:

<input type="submit" name="submit" value="Submit" 
    onClick="return pass(this.form.oldPass.value;">

Now i've tried adding the 'this.form' part three times because thats how many parameters the function would take but it just doesn't seem to do anything:

现在我已经尝试添加 'this.form' 部分三次,因为那是函数需要多少参数,但它似乎没有做任何事情:

<input type="submit" name="submit" value="Submit" 
    onClick="return pass(this.form.oldPass.value,
                         this.form.newPass.value, 
                         this.form.newPassCheck);">

Is this the right way to go about passing form data to a script? is there another way around it or am i just doing something wrong here?

这是将表单数据传递给脚本的正确方法吗?有没有其他方法可以解决它,或者我只是在这里做错了什么?

thanks for the help, if you need anymore code i'll add it.

感谢您的帮助,如果您需要更多代码,我会添加它。

回答by under5hell

try this one. (you need jqueryto do this.)

试试这个。(你需要jquery来做到这一点。)

<form>
 old pass <input type="password" id="old_pass" />
 new pass <input type="password" id="new_pass" />
 new pass confirmation <input type="password" id="new_pass_confirm"/>
</form>

in your .js file , write this:

在你的 .js 文件中,写下这个:

$(document).ready(function(){
    var old_pass = $('#old_pass').val();
    var new_pass = $('#new_pass').val();
    var new_pass_confirm = $('#new_pass_confirm').val();

    //then call your method with those vars as parameter
    anyMethod(old_pass, new_pass, new_pass_confirm);

});

//this is your function
function anyMethod(old_pass, new_pass, new_pass_confirm)
{
  //put your validation code or ajax call here...
}

回答by Sudhir Bastakoti

Add idfor each of your input boxes and you can get value in function, like:

为每个输入框添加id,您可以获得函数值,例如:


function pass() {
 var old = document.getElementById("yourOldPassId").value;
 var new = document.getElementById("yourNewPassId").value;
 var newConfirm  = document.getElementById("yourNewConfirmPassId").value;
}

Did you mean something like this

你的意思是这样吗

回答by zozo

Give the inputs some ids and get the data from there.

为输入提供一些 ID 并从那里获取数据。

Something like:

就像是:

<input type bla bla id="whateverId0" />

For each input.

对于每个输入。

The submit should have something like onclick="whateverFunction()"

提交应该有类似 onclick="whateverFunction()"

The js should look like:

js 应该是这样的:

     function whateverFunction()
     {
          val0 = document.getElementById('whateverId0');
          // For all 3 values.

          // Do whatever you want with them.
     }

回答by Adam Arold

I believe that you should add an onSubmit listener to your form. There are good js frameworks out there like jquery which are easy to use. That way you can make a listener which runs a function when you submit your form. In that function you can extract the data to json for example and ajax it to your server.

我相信您应该在表单中添加一个 onSubmit 侦听器。有一些很好的 js 框架,比如 jquery,它们很容易使用。这样你就可以制作一个在你提交表单时运行一个函数的监听器。例如,在该函数中,您可以将数据提取到 json 并将其 ajax 发送到您的服务器。

info about jquery

关于 jquery 的信息

jquery ajax

jQuery ajax

how to extract form data

如何提取表单数据