Javascript 使用 onClick 传递多个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11513917/
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-08-24 06:06:01  来源:igfitidea点击:

Passing multiple variables with onClick

javascript

提问by Jaybest

I am wanting many different buttons (each written in using PHP), to pass 3 or more variables using onClick to the function to then write this into the input fields.

我想要许多不同的按钮(每个按钮都是用 PHP 编写的),使用 onClick 将 3 个或更多变量传递给函数,然后将其写入输入字段。

The following is getting close, but I am not sure how to pass up the multiple var array up to the function for the re-write..

以下内容越来越接近,但我不确定如何将多个 var 数组传递给重写函数。

<HTML>
<HEAD>
<TITLE>Test Input </TITLE>
<SCRIPT LANGUAGE="JavaScript">
function writeText (var1, var2, var3) {
    form.inputbox1.value = var1
    form.inputbox2.value = var2
    form.inputbox3.value = var3
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="myform" ACTION="" METHOD="GET">
Enter something in the box: <BR>
<INPUT TYPE="button" NAME="button2" Value="Write" onClick="writeText(this.form)"><br>

  <input type="text" name="inputbox2"><br>
  <input type="text" name="inputbox3"><br>
  <input type="text" name="inputbox4">

</FORM>
</BODY>
</HTML>

回答by SimeonJM

If what you are trying to do is have different buttons, each of which writes a different set of values to the three fields, then there are a couple of issues.

如果您尝试使用不同的按钮,每个按钮将一组不同的值写入三个字段,则存在一些问题。

  1. In the writeText function, you are not specifying which form should be written to (unless the 'form' variable contains the form, that is).
  2. The parameter you are passing to the writeText function is not actually one you have written the writeText function to use.
  1. 在 writeText 函数中,您没有指定应写入哪个表单(除非 'form' 变量包含该表单)。
  2. 您传递给 writeText 函数的参数实际上并不是您编写的要使用的 writeText 函数的参数。


First, to write three values to the fields using the writeText function conceptually like you've written it, your button HTML should be along the lines of:


首先,要在概念上像您编写的那样使用 writeText 函数将三个值写入字段,您的按钮 HTML 应该是这样的:

<INPUT TYPE="button" NAME="button2" Value="Write" onClick="writeText('value1','value2','value3')">

Also, you need to change the code in your 'writeText' function to point to the fields you want. One way of doing this would be to add an ID to the fields you want to write to, and then getElementById, e.g. change your fields to

此外,您需要更改“writeText”函数中的代码以指向所需的字段。这样做的一种方法是将 ID 添加到您要写入的字段,然后 getElementById,例如将您的字段更改为

<input type="text" name="inputbox1" id="inputbox1">

then in the javascript using:

然后在 javascript 中使用:

var inputbox1 = document.getElementById('inputbox1');
if (inputbox1) { inputbox1.value = var1 }

So specifically, to pass the three values from the onClick to the writeText function, you need to use

所以具体来说,要将 onClick 中的三个值传递给 writeText 函数,您需要使用

onClick="writeText('value to write 1','value to write 2', 'value to write 3');"
而不是
writeText(this.form)

回答by N.Yotsov

And how to handle -

以及如何处理——

$('#list_tasks').append('<button class="btn btn-default" onclick="buildMemberPerTask(' + res + ',' + res + ')"; type="submit">' + res.results[i].description + '</button>');