如何将 2 个或更多参数从 HTML 输入传递给 Javascript 函数

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

How to pass 2 or more arguments from HTML inputs to a Javascript function

javascripthtmlinputarguments

提问by Ivo San

I have these inputs:

我有这些输入:

<input type='radio' id='1' name='s' onclick='pieces(this.value);'  value='6'>6
<input type='radio' id='2' name='s' onclick='pieces(this.value);'  value='12'>12
<input type='radio' id='3' name='s' onclick='pieces(this.value);'  value='24'>24
<input type='radio' id='11' name='v' onclick='pieces(this.value);'  value='yes'>Yes
<input type='radio' id='12' name='v' onclick='pieces(this.value);'  value='no'>No

I want to pass the value from "name='s'" to var1, and value from "name='v'" to var2 to the function pieces(var1, var2)when the radio button is clicked.

pieces(var1, var2)当单击单选按钮时,我想将值从“name='s'”传递给 var1,并将值从“name='v'”传递给 var2 到函数。

回答by Mayank

You can get values of both radio inputs in Javascript method itself using document.getElementsByName()method or this should also work,
Try using func(document.getElementsByName('s'),document.getElementsByName('v'))

您可以使用document.getElementsByName()方法在 Javascript 方法本身中获取两个无线电输入的值,或者这也应该有效,
请尝试使用func(document.getElementsByName('s'),document.getElementsByName('v'))

Below code should help,

下面的代码应该有帮助,

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="js1/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
    alert('Ok');
    $('#btn').click(function(){
        var r1=$('input:radio[name=s]').val();
        var r2=$('input:radio[name=v]').val();
        show(r1,r2);
    });
});

function show(r1,r2){
    alert(r1);
    alert(r2);
}
</script>
</head>
<body>
<input type='radio' id='1' name='s' onclick='pieces(this.value);'  value='6'>6
<input type='radio' id='2' name='s' onclick='pieces(this.value);'  value='12'>12
<input type='radio' id='3' name='s' onclick='pieces(this.value);'  value='24'>24


<input type='radio' id='11' name='v' onclick='pieces(this.value);'  value='yes'>Yes
<input type='radio' id='12' name='v' onclick='pieces(this.value);'  value='no'>No

<button id="btn">Submit</button>
</body>
</html>

回答by Antguider

You can pass the arguments in the following different ways:

您可以通过以下不同方式传递参数:

function antguider(name, url){
    alert("Name = "+name+" URL = "+url);
}

HTML Part

HTML 部分

<input type="button" onclick=antguider('AntGuider','http://antguider.blogspot.com') value="Click Without Quotation" />
<input type="button" onclick="antguider('AntGuider','http://antguider.blogspot.com')" value="Click With Quotation" />
<input type="button" onclick="antguider(12345,'http://antguider.blogspot.com')" value="No Quotation Needed For Button" />

回答by Mohit Pandey

For 2 inputs, this can easily be done with JQuery.

对于 2 个输入,这可以使用 JQuery 轻松完成。

va1=$("input[name=s]").val();
va2=$("input[name=v]").val();

Then call the desired function, passing in your values as parameters.

然后调用所需的函数,将您的值作为参数传递。

func(va1,va2);