简体   繁体   English

单选按钮Javascript数组

[英]Radio Button Javascript Array

I have a radio button like below 我有一个像下面这样的单选按钮

Apple
<input type="radio" id="one" name="apple" data-price="10" value="light"/> Light
<input type="radio" id="two" name="apple" data-price="20" value="dark" /> Dark
<input type="text" id="appleqty" name="appleqty" value="" />

Mango
<input type="radio" id="three" name="Mango" data-price="30" value="light"/> Light
<input type="radio" id="one" name="Mango" data-price="40" value="dark" /> Dark
<input type="text" id="Mangoqty" name="Mangoqty" value="" />

Pine Apple
<input type="radio" id="four" name="Pineapple" data-price="50" value="light"/> Light
<input type="radio" id="five" name="Pineapple" data-price="60" value="dark" /> Dark
<input type="text" id="Pineappleqty" name="Pineappleqty" value="" />

Grape
<input type="radio" id="six" name="Grape" data-price="70" value="light"/> Light
<input type="radio" id="seven" name="Grape" data-price="80" value="dark" /> Dark
<input type="text" id="Pineappleqty" name="Pineappleqty" value="" />

I this i need to create a array like below 我需要创建一个如下所示的数组

   array
      0 => 
        array
          'apple' => string 'light' (length=10)
          'price' => string '50' (length=1)
      1 => 
        array
          'Pineapple' => string 'dark' (length=10)
          'price' => string '60' (length=1)

A Kind Note : the array key should be the radio button name, the price should be taken from data-price in radio button and i need to serilize this array This should be done using javascript. 一种注意the array key should be the radio button name, the price should be taken from data-price in radio button ,我需要对此数组进行serilize这应该使用javascript完成。

You'll need to iterate the <input> s. 你需要迭代<input> In order to do so: 为此:

  1. If you know beforehand the element name s you'll be looking for (ie apple , Mango , and so on), you can do document.getElementsByName on each of those names, as explained in In JavaScript, how can I get all radio buttons in the page with a given name? 如果您事先知道要查找的元素name (例如appleMango等),则可以对每个名称执行document.getElementsByName ,如在JavaScript中所述,如何获取所有单选按钮在具有给定名称的页面中? .

  2. If you don't know them (they are dynamic), just wrap them in a <div> with a given id and iterate its children, as in how to loop through some specific child nodes . 如果你不了解它们(它们是动态的),只需将它们包装在具有给定id的<div>并迭代它的子节点 ,就像如何遍历某些特定的子节点一样

While iterating the radios of a given name , inspect the checked property of each in order to know which of them is selected, as in How can i check if a radio button is selected in javascript? 在迭代给定name的无线电时,检查每个的已checked属性,以便知道它们中的哪一个被选中,如我如何检查是否在javascript中选择了一个单选按钮? .

You'll want to build an associative array with keys the following key-value pairs, as in Dynamically creating keys in javascript associative array : 您将需要使用以下键值对的键构建关联数组 ,如在javascript关联数组动态创建键

  1. <input> 's name and value attributes. <input>namevalue属性。
  2. literal price and data-price attribute. 字面pricedata-price属性。 You'll need to use getAttribute('data-price') to get the data-price attribute's value. 您需要使用getAttribute('data-price')来获取data-price属性的值。

Example: 例:

function createArray() {

    var myArr = new Array();
    myArr[0] = createSubArray('apple');
    myArr[1] = createSubArray('Mango');
    myArr[2] = createSubArray('Pineapple');
    myArr[3] = createSubArray('Grape');
    return myArr;
}


function createSubArray(name) {
    var arr = new Array();
    elems = document.getElementsByName(name);
    for (var i = 0; i < elems.length; i++) {
        if (elems[i].checked) {
            arr[name] = elems[i].value;
            arr['price'] = elems[i].getAttribute('data-price');
        }
    }
    return arr;
}​

You can see this example working in this JSFiddle . 您可以在此JSFiddle中看到此示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM