English 中文(简体)
How can I convert a select element to radio buttons with jQuery?
原标题:

I am trying to convert select boxes to radio buttons on the fly using jQuery, and I m not sure the best way.

Example HTML:

  <form id="product">    
    <select name="data[123]">
      <option value="1">First</option>
      <option value="2">Second</option>
      ......
      <option value="n">xxxxxx</option>
    </select>
  </form>

I want to convert it at page load using jquery to:

<form id="product">
  <input type="radio" name="data[123]" value="1" />
  <label for="data[123]">First</label><br/>
  <input type="radio" name="data[123]" value="2" />
  <label for="data[123]">Second</label><br/>
  ......
  <input type="radio" name="data[123]" value="n" />
  <label for="data[123]">xxxxxx</label><br/>
</form>

And it needs to be dynamic so it will loop dynamically for each select box and each option inside (as different products have different options)

I m trying to figure the best way. Either to get a multidimensional array of all the values first and then build the radio buttons.. or swap them out one at a time in a loop. Currently attempting the former, but I think I may be overthinking it:

$(document).ready(function(){
    
    //Get Exising Select Options    
    $( form#product select ).each(function(i, select){
        $(select[i]).find( option ).each(function(j, option){
            //alert($(option).text());
            option[j] = [];
            option[j][ text ] = $(option).text();
            option[j][ val ] = $(option).val();
        });
    });
    
    
    //Remove Select box
    $( form#product select ).remove();
});

Has anyone tried this or have some secret/easier way of doing it that I m missing?

最佳回答

I put this together, and tested it in a few browsers, all seem to handle it well. It will take the data out of the <option> elements and create the <input/><label/><br/> for each one, then remove the select box.

//Get Exising Select Options    
$( form#product select ).each(function(i, select){
    var $select = $(select);
    $select.find( option ).each(function(j, option){
        var $option = $(option);
        // Create a radio:
        var $radio = $( <input type="radio" /> );
        // Set name and value:
        $radio.attr( name , $select.attr( name )).attr( value , $option.val());
        // Set checked if the option was selected
        if ($option.attr( selected )) $radio.attr( checked ,  checked );
        // Insert radio before select box:
        $select.before($radio);
        // Insert a label:
        $select.before(
          $("<label />").attr( for , $select.attr( name )).text($option.text())
        );
        // Insert a <br />:
        $select.before("<br/>");
    });
    $select.remove();
});
问题回答

You re on the right track. Iterate and collect the select data into variables and make as few DOM operation calls as possible (for efficiency) to create the radio inputs.





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签