Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript HTML如何在单选按钮上传递两个变量?_Javascript_Html_Dom_Onclick_Parameter Passing - Fatal编程技术网

Javascript HTML如何在单选按钮上传递两个变量?

Javascript HTML如何在单选按钮上传递两个变量?,javascript,html,dom,onclick,parameter-passing,Javascript,Html,Dom,Onclick,Parameter Passing,如何在按下按钮时传递两个变量?这似乎很基本,但我完全被卡住了 我正在创建一个在左侧有一个单选按钮的表,当您按下它时,我想要检索两个值。我当前正在检索一个值,但缺少另一个值 目前,我正在向onRadioButtonPress函数传递一个值,如下所示: html += "<tr><td><center><br> <input type='radio' onclick='onRadioButtonP

如何在按下按钮时传递两个变量?这似乎很基本,但我完全被卡住了

我正在创建一个在左侧有一个单选按钮的表,当您按下它时,我想要检索两个值。我当前正在检索一个值,但缺少另一个值

目前,我正在向onRadioButtonPress函数传递一个值,如下所示:

 html += "<tr><td><center><br>
          <input type='radio' 
             onclick='onRadioButtonPress(this.value)' 
             name='siteChosen' 
             value='"+siteIDArray[i]+"'>
           <center></td>";
//function for on radio button press
   function onRadioButtonPress(val){

      //blah blah blah
 }
这很好,但是我如何添加第二个值

usagePlanArray[i]

到我的onClick函数?如何更改输入

提前谢谢!!!:)请让我知道如果你有任何问题,或如果我错过了一些可以帮助你


解决了

请参阅@mplungjan的标记答案。我使用了对jQuerys有用的替代方法。

用JSON传递数组:

   value='"+JSON.stringify(siteIDArray)+"'>
这将作为JSON数组字符串传递整个数组

传递到单个值:

    value='["+siteIDArray[i] + "," + usagePlanArray[i] + "]'>
或者,作为一个对象:

    value='{ \"val1\":\""+siteIDArray[i] + "\",\"val2\":\"" + usagePlanArray[i] + "\"]'>
现在在函数中解码

   values = JSON.parse(val);
   ...
   xyz = values[0];  // for the array
   ...
   xyz = values.val1; // for the object
如果直接将代码传递给onclick而不是value参数,那么代码甚至可以进行优化,但这可能有一些额外的用途

value='"+siteIDArray[i]+"|"+usagePlanArray[i]+"'>
但是您也需要从HTML中删除换行符

然后在函数中可以使用

var vals=val.split("|");
var siteID=vals[0], usagePlan=vars[1];
备选方案-在jQUery中特别有用:

html += "<tr><td><center><br><input type='radio'"+
        " onclick='onRadioButtonPress(this)' name='siteChosen'"+
        " data-siteid='"+siteIDArray[i]+"'"+
        " data-usageplan='"+usagePlanArray[i]+"'><center></td>";

如果对两个数组使用相同的索引,为什么不将索引存储为值并在单击回调中查找所需的值

var html= "";

var siteIDArray = [1, 5, 2, 33];
var usagePlanArray = ["unlimited", "20gb", "10gb", "5gb" ]

function arrayCombo(index){
    return {siteId: siteIDArray[index], usagePlan: usagePlanArray[index]};   
}

for(var i=0; i<siteIDArray.length; i++){
    html += "<tr><td><center><br><input type='radio' name='siteChosen' value="+i+"><center></td>";
}

$('#content').html(html);

$('input').on('click', function(event){
    //Do whatever you want with the values you are looking for here
    var x = arrayCombo(this.value);
    alert(x.siteId+", "+x.usagePlan);
});
var html=”“;
var siteidaray=[1,5,2,33];
var usagePlanArray=[“无限”、“20gb”、“10gb”、“5gb”]
函数arrayCombo(索引){
返回{siteId:siteidaray[index],usagePlan:usagePlanArray[index]};
}

对于(var i=0;我仍然有点困惑。那么我可以像这样使用JSON:value='“+JSON.stringify(siteIDArray[i],usagePlanArray[i])+”>让我试试看它是如何运行的。在这种情况下,值[0]是siteIDArray[i],值[1]是usagePlanArray[i]这太复杂了this@MadsterMaddnessYepp.wt是第二个值??我可以问一些愚蠢的问题吗?为什么要将“rad”传递给函数?是因为它是一个单选按钮吗?是的。注意(这个)在callIt中成功了!你太棒了!请在data usageplan之后在html中添加等号!是的,我在手机上,字符串很长:}静态数组只是示例中的占位符。它们的值可以用ajax调用的结果替换,也可以用提供数据的方式替换。
var html= "";

var siteIDArray = [1, 5, 2, 33];
var usagePlanArray = ["unlimited", "20gb", "10gb", "5gb" ]

function arrayCombo(index){
    return {siteId: siteIDArray[index], usagePlan: usagePlanArray[index]};   
}

for(var i=0; i<siteIDArray.length; i++){
    html += "<tr><td><center><br><input type='radio' name='siteChosen' value="+i+"><center></td>";
}

$('#content').html(html);

$('input').on('click', function(event){
    //Do whatever you want with the values you are looking for here
    var x = arrayCombo(this.value);
    alert(x.siteId+", "+x.usagePlan);
});