开发者

array of radio - jquery

开发者 https://www.devze.com 2023-02-25 15:06 出处:网络
i have the cod开发者_运维百科e below and it shows a list of radio buttons My question is how i can make a list of buttons, the equivalent for this in an array mode (jquery)

i have the cod开发者_运维百科e below and it shows a list of radio buttons My question is how i can make a list of buttons, the equivalent for this in an array mode (jquery)

  <input type="radio" name="name1" value="450" /> 450€<br />
  <input type="radio" name="name2" value="500" /> 500€<br />
  <input type="radio" name="name3" value="550" /> 550€<br />
  <input type="radio" name="name4" value="600" /> 600€<br />
  <input type="radio" name="name5" value="650" /> 650€<br />
  <input type="radio" name="name6" value="700" /> 700€<br />

this simple make an array without change anything. And another question, how i will can access the values when i will have this in an array mode?

thanks


another jQuery solution:

Live Demo

<ul>
    <li id="lista"></li>
</ul>

<script type="text/javascript">
$(document).ready(function() {
    var start = 450;
    for (var x=0; x<=20; x++) {
        var item = document.createElement('input');
            $(item).attr('type', 'radio');
            $(item).attr('id', 'price'+x+1);    // numbered starting from 1
            $(item).attr('name', 'price');
            $(item).attr('value', start + (x * 50));
        var label = document.createElement('label');
            $(label).attr('for', 'price'+x+1);  // numbered starting from 1
            $(label).html(start + (x * 50) + '€');
        $('#lista').append(item);
        $('#lista').append(label);
        $('#lista').append('<br>');
    }
});
</script>


This will create 20 inputs and increment value and text by 50

var count = 20;
var increment = 50;

for (y = 0; y < count; y++) {
    var inp = '<input type="radio" name="name1" value="' + increment + '" /> ' + increment + '<br />';
    $(inp).appendTo('#lista');
    increment = increment + 50;
}

Check working example at http://jsfiddle.net/fxmmN/1/


Hopefully this will guide you in the right direction! I built a simple function create as many radios as you specify and increment the value by 50. Try modifying this a little to get to your desired solution!

function populateRadios(){
  var i = 0,
      numberOfRadios = 3,
      baseNumber = 0;
    for(i; i<numberOfRadios; i++){
        var aRadio = document.createElement('input');
        aRadio.type = 'radio'
        aRadio.name = 'radio1';
        aRadio.id = 'radio' + i;
        baseNumber += 50;
        aRadio.value = baseNumber

       $('#theForm').append(aRadio)
    } 
}
populateRadios()
0

精彩评论

暂无评论...
验证码 换一张
取 消