admin管理员组

文章数量:1357716

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.

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.

Share Improve this question edited Jun 30, 2012 at 9:33 dda 6,2132 gold badges27 silver badges35 bronze badges asked Jun 30, 2012 at 5:40 Ivo SanIvo San 1,2534 gold badges16 silver badges26 bronze badges 1
  • @arttronics, already editted. thanks – Ivo San Commented Jun 30, 2012 at 8:35
Add a ment  | 

3 Answers 3

Reset to default 1

You can pass the arguments in the following different ways:

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

HTML Part

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

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

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>

For 2 inputs, this can easily be done with 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);

本文标签: How to pass 2 or more arguments from HTML inputs to a Javascript functionStack Overflow